Author: Tomas Rutkauskas
How to select or find a TTreeView node by caption
Answer:
Solve 1:
1 2 function GetNodeByCaption(NodeCaption: string): TTreeNode;
3 var4 X: Integer;
5 begin6 Result := nil;
7 for X := 0 to TreeView1.Items.Count - 1 do8 begin9 if (TreeView1.Items[X].Caption = NodeCaption) then10 Result := TreeView1.Items[X];
11 Break;
12 end;
13 end;
Solve 2:
Returns a node based on the text property. Set AVisible to show the new node:
14 15 function GetNodeByText(ATree: TTreeView; AValue: string;
16 AVisible: Boolean): TTreeNode;
17 var18 Node: TTreeNode;
19 begin20 Result := nil;
21 if ATree.Count = 0 then22 Exit;
23 Node := ATree.Items[0];
24 while Node <> nildo25 begin26 if UpperCase(Node.Text) = AValue then27 begin28 Result := Node;
29 if AVisible then30 Result.MakeVisible;
31 Break;
32 end;
33 Node := Node.GetNext;
34 end;
35 end;