Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Adding nodes to a Treeview at runtime

Posted on 2001-06-10
8
2,395 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:MaxSCZ
ID: 6177999
Hi,
try with:

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

bye Max
0
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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