Link to home
Start Free TrialLog in
Avatar of henrylim
henrylim

asked on

Adding nodes to a Treeview at runtime

How can I use 'Add' or AddChild to a selected TreeNode at runtime?
Avatar of Mohammed Nasman
Mohammed Nasman
Flag of Palestine, State of image

procedure TForm1.Button1Click(Sender: TObject);
var
  MyNode : TTreeNode;
begin
  TreeView1.Items.Add(MyNode,'Test');
end;
Avatar of bugroger
bugroger

Var
 Node1, Node2 : TTreeNode;

Begin
 Node1 := TreeView1.Add(NIL, '1st Node');
 Node1.ImageIndex := 1; //If you have set the ImageList

 Node2.TreeView1.Add(Node1, '1st SubNode');
 Node2.ImageIndex := 1;

 Node2.TreeView1.Add(Node1, '2nd SubNode');
 Node2.ImageIndex := 2;

 Node2.TreeView1.Add(Node2, '3rd SubNode');
 Node2.ImageIndex := 4;

 Node1 := TreeView1.Add(NIL, '2nd Node');
End;

Result
=======

- 1st Node
  |
   - 1st SubNode
  |
   - 2nd SubNode
     |
      - 3rd SubNode

- 2nd Node  

 







Avatar of henrylim

ASKER

Using 'Add' is not problematic, but I need to use 'AddChild' to add a child node to a selected node at wish.
Hi,
try with:

TreeView1.Items.AddChild(TreeView1.Selected,'My Child Node');

bye Max
Sorry, Max.  It doesn't help.
ASKER CERTIFIED SOLUTION
Avatar of bugroger
bugroger

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, bugroger,

Bassically your Double-click event is what I want.  I assign the new child node to ANode variable and assign an image index to it, but when the child node is selected, its image changes to the parent's image.  When it is unselected, it has its own image back again.  Why?

Thanks.    

Node2.ImageIndex    := 4;
Node2.SelectedIndex := 3;

ImageIndex    -> Image that will be shown when the
                 Item is not selected
SelectedIndex -> Image that will be shown when the
                 Item is selected

Good Luck,

Peter