Solved

Adding nodes to a Treeview at runtime

Posted on 2001-06-10
8
2,169 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:mnasman
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

21 Experts available now in Live!

Get 1:1 Help Now