1. Home
  2. Computing & Technology
  3. Delphi Programming
Get TreeView Node By Text
How to locate (search and select) a TreeView node given by node text using Delphi.
 More of this Feature
• Part 2
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• VCL Using articles
• Beginners Delphi Section

Many times while developing Delphi applications using the TreeView component I've bumped into a situation to need to search for a tree node given by only the text of the node.

In this article I'll present you with one quick and easy function to get TreeView node by text.    

A Delphi example
First, we'll build a simple Delphi form containing a TreeView, a Button, CheckBox and an Edit component - leave all the default component names.

Delphi : Get TreeView Node by Text

As you might imagine, the code will work something like:
if GetNodeByText given by Edit1.Text returns a node and MakeVisible (CheckBox1) is true then select node.

The most important part is the GetNodeByText function:

This function simply iterates through all the nodes inside the ATree TreeView starting from the first node (ATree.Items[0]). The iteration uses the GetNext method of the TTreeView class to look for the next node in the ATree (looks inside all nodes of all child nodes). If the Node with text (label) given by AValue is found (case insensitive) the function returns the node. The boolean variable AVisible is used to make the node visible (if hidden).

function GetNodeByText
(ATree : TTreeView; AValue:String; 
 AVisible: Boolean): TTreeNode;
var
    Node: TTreeNode;
begin
  Result := nil;
  if ATree.Items.Count = 0 then Exit;
  Node := ATree.Items[0];
  while Node <> nil do
  begin
    if UpperCase(Node.Text) = UpperCase(AValue) then
    begin
      Result := Node;
      if AVisible then
        Result.MakeVisible;
      Break;
    end;
    Node := Node.GetNext;
  end;
end;

This is the code that runs the 'Find Node' button OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
var
  tn : TTreeNode;
begin
   tn:=GetNodeByText(TreeView1,Edit1.Text,CheckBox1.Checked);
   if tn = nil then
    ShowMessage('Not found!')
   else
    begin
      TreeView1.SetFocus;
      tn.Selected := True;
    end;
end;

Note: If the node is located the code selects the node, if not a message is displayed.

That's it! As simple as only Delphi can be. However, if you look twice, you'll see something is missing: the code will find the FIRST node given by AText! What if you want to search for a node at the same level as the calling node - where this calling node is also provided to the function!
I'll leave the question to be answered on our Delphi Programming Forum.

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.