Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delphi TreeView - create nodes at run-time

Can anybody tell me how to do the following:

  • Create Nodes
  • Enable/Disable Individual Nodes

I want to know how to do the above at Application run-time, eg in the Form's OnCreate event.

like image 732
Remus Rigo Avatar asked Jan 29 '10 16:01

Remus Rigo


1 Answers

@Remus, here you have an simple example adding nodes.

Adding a root Node (level 0)

Var
  Node : TTreeNode;
begin
   //function TTreeNodes.Add(Sibling: TTreeNode; const S: string): TTreeNode;
   Node:=TreeView1.Items.Add(nil,'My Root Node') ;
   Node.ImageIndex:=0;//now you can change any  property of the node
end;

Adding a child Node (level > 0)

//in this case we add a child node in the current selected node.
    Var
      Node : TTreeNode;
    begin       
       if TreeView1.Selected= nil then exit;       
       Node:=TreeView1.Items.AddChild(TreeView1.Selected,'My Child Node') ;
       Node.ImageIndex:=0;//now you can change any property of the node
    end;

Adding many nodes

if you wanna add many nodes using a loop or something else you must use BeginUpdate before making the changes to the treeview. When all changes are complete, call EndUpdate to show the changes on screen. BeginUpdate and EndUpdate prevent excessive redraws and speed up processing time when nodes are added, deleted, or inserted.

Var
  Node : TTreeNode;
  i    : Integer;
begin
  TreeView1.Items.BeginUpdate;
  try
   for i:=1 to 100 do
   begin
    Node:=TreeView1.Items.Add(nil,'My Root Node '+IntToStr(i)) ;
    Node.ImageIndex:=0;
   end;
  finally
  TreeView1.Items.EndUpdate;
  end;
end;

About disable a node, does not exist any property like that.

like image 121
RRUZ Avatar answered Sep 28 '22 00:09

RRUZ