2004. május 31., hétfő

Select or find a TTreeView node by caption


Problem/Question/Abstract:

How to select or find a TTreeView node by caption

Answer:

Solve 1:

function GetNodeByCaption(NodeCaption: string): TTreeNode;
var
  X: Integer;
begin
  Result := nil;
  for X := 0 to TreeView1.Items.Count - 1 do
  begin
    if (TreeView1.Items[X].Caption = NodeCaption) then
      Result := TreeView1.Items[X];
    Break;
  end;
end;


Solve 2:

Returns a node based on the text property. Set AVisible to show the new node:

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

Nincsenek megjegyzések:

Megjegyzés küldése