Get TreeView Node By Text(TreeView上查找)

How to locate (search and select) a TreeView node given by node text using Delphi. 

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!
Intro...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!...

The above text issued a challenge to enhance the TreeView search algorithm introduced in the part 1 of this article: Get TreeView Node By Text.

Remember, we had a treeview component "filled" with nodes. The problem was to search (locate and make visible) the treeview node in a function that receives the node text as the parameter.

Look the picture below, we have a node with the caption "Button" under the "Standard" node. If we are to have the "Button" node below (as a child node) the "Additional" node, the function will find the first "Button" - the one under "Standard".

What we now want, is to tell the function to search for "Button" under "Additional"...

 

Delphi : Get TreeView Node by Text

We enhance the GetNodeByText function by adding one more parameter - the "InNode" node.

function GetNodeByText
  (ATree : TTreeView; InNode : TTreeNode; 
   AValue:String; AVisible:boolean) : TTreeNode;
var
  NodeText : String;
  NextNode : TTreeNode;
begin
  Result := nil;
  NextNode := InNode.GetFirstChild;
  while NextNode <> nil do
  begin
    NodeText := NextNode.Text;
    if UpperCase(NodeText) = UpperCase(AValue) then
    begin
       NextNode.Selected := AVisible;
       Break;
    end
    else
      NextNode := NextNode.getNextSibling;
  end; //while
  Result := NextNode;
end;

 

The function simply iterates through all the first level child nodes of the provided "InNode" node and looks for a node labeled AValue.

 

The code that runs the 'Find Node' button OnClick event is slightly modified. The second parameter is the "root" node for the search - for example the selected node.

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

That's it. If you have any questions please use the Delphi Programming Forum.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值