Solved

find a node in VST

Posted on 2016-09-09
2
47 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now