Solved

Adding nodes to a Treeview at runtime

Posted on 2001-06-10
8
2,252 Views
Last Modified: 2011-10-03
How can I use 'Add' or AddChild to a selected TreeNode at runtime?
0
Comment
Question by:henrylim
8 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6173163
procedure TForm1.Button1Click(Sender: TObject);
var
  MyNode : TTreeNode;
begin
  TreeView1.Items.Add(MyNode,'Test');
end;
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6173263
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  

 







0
 

Author Comment

by:henrylim
ID: 6177333
Using 'Add' is not problematic, but I need to use 'AddChild' to add a child node to a selected node at wish.
0
 
LVL 1

Expert Comment

by:MaxSCZ
ID: 6177999
Hi,
try with:

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

bye Max
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:henrylim
ID: 6179713
Sorry, Max.  It doesn't help.
0
 
LVL 2

Accepted Solution

by:
bugroger earned 20 total points
ID: 6180606
Here are some example:

//basics
procedure TForm1.Button1Click(Sender: TObject);
Var
Node1, Node2 : TTreeNode;

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

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

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

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

 Node1 := TreeView1.Items.AddChild(NIL, '2nd Node');
 Node1.HasChildren := TRUE; //To use onExpanding - Event
end;


//To add a childTreeNode OnChange - Event
//Just select an item
procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
begin
 TreeView1.Items.AddChild(Node, IntToStr(Random(100)));
end;

//To add a ChildTreeNode onDblClich - Event
//dblclick on an item
procedure TForm1.TreeView1DblClick(Sender: TObject);
begin
 TreeView1.Items.AddChild(TTreeView(Sender).selected, IntToStr(Random(100)));
end;



//IF TreeNode "HasChildren" then you can use the
//onExpanding - Event to add a Child.
//-> like an Explorer

//Set TreeNode "HasChildren".
//Node1 := TreeView1.Items.AddChild.............
//Node1.HasChildren := TRUE;

procedure TForm1.TreeView1Expanding(Sender: TObject; Node: TTreeNode;
  var AllowExpansion: Boolean);
begin
  TreeView1.Items.AddChild(Node, IntToStr(Random(100)));
end;
0
 

Author Comment

by:henrylim
ID: 6181815
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.    
0
 
LVL 2

Expert Comment

by:bugroger
ID: 6182739

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
                   



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
add combobox item based on numbers 9 125
URL for downloading Google Chrome for Win XP 2 125
LAN or WAN ? 11 82
creating threads in delphi 1 84
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now