henrylim
asked on
Adding nodes to a Treeview at runtime
How can I use 'Add' or AddChild to a selected TreeNode at runtime?
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
Node1, Node2 : TTreeNode;
Begin
Node1 := TreeView1.Add(NIL, '1st Node');
Node1.ImageIndex := 1; //If you have set the ImageList
Node2.TreeView1.Add(Node1,
Node2.ImageIndex := 1;
Node2.TreeView1.Add(Node1,
Node2.ImageIndex := 2;
Node2.TreeView1.Add(Node2,
Node2.ImageIndex := 4;
Node1 := TreeView1.Add(NIL, '2nd Node');
End;
Result
=======
- 1st Node
|
- 1st SubNode
|
- 2nd SubNode
|
- 3rd SubNode
- 2nd Node
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(T reeView1.S elected,'M y Child Node');
bye Max
try with:
TreeView1.Items.AddChild(T
bye Max
ASKER
Sorry, Max. It doesn't help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
var
MyNode : TTreeNode;
begin
TreeView1.Items.Add(MyNode
end;