Solved

find a node in VST

Posted on 2016-09-09
2
79 Views
Last Modified: 2016-09-21
i am using this function to find node in treeview

function TFORM1.lookingTreeView(name: String): PVirtualNode;
var
Node: PVirtualNode;
Data: PUserData;
begin
Result := nil;
Node := VDT1.GetFirst;
while ((Node <> nil) and (Result = nil)) do
begin
Data := VDT1.GetNodeData(Node);
if (Data.FObject.name= name) then
begin
Result := Node;
break;
end
else
Node := VDT1.GetNext(Node);
end;
end;

Open in new window


and i use it like so
Node := lookingTreeView(namedata);

if Node <> nil then
begin
// then stop execute the code 
exit;
end;
// if its nil then do execute the rest of code

Open in new window


so this code search for node in Vst and if the node exits stop the code execution and if does not exits do the rest  

but some times this function got bypassed i mean when a node is already exits the if statement of node <> nil does not detect that its not nil and continue executing the code.
0
Comment
Question by:dolphin King
  • 2
2 Comments
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 41792114
VST ?
Very Strong Tree ?
or a Virtually Silly Teacup ?
It helps in being explicit of what component  you use ...

don't like indenting ?
i use it to find memory leaks ...
never bothered about that ?
it also helps to make code more readable

Using SameText to search case insensitive

you got this code here: ?
http://www.delphipages.com/forum/showthread.php?t=152084

no point in checking for (Result = nil) in the while loop
That never happens

If you add the tree as a parameter, you can use this function from a common unit

function lookingTreeView(vst: VirtualStringTree; name: String): PVirtualNode;
var
  Node: PVirtualNode;
  Data: PUserData;
begin
  Result := nil;
  Node := vst.GetFirst;
  while Node <> nil do
  begin
    Data := vst.GetNodeData(Node);
    if SameText(Data.FObject.name, name) then
    begin
      Result := Node;
      break;
    end else
      Node := vst.GetNext(Node);
  end;
end;

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 41792118
an exit in the middle of a procedure ?
why not reverse the logic ?

begin
  Node := lookingTreeView(namedata);
  if Node = nil then
  begin
    // if its nil then do execute the rest of code
  end;
end;

Open in new window

1

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Math Equation 23 128
WordPress: Debugging from my Windows 10 Desktop 6 67
Way to create an iPhone app for my customers 8 47
Delphi Firemonkey : user drawing in window 3 27
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question