Solved

Adding nodes to a Treeview at runtime

Posted on 2001-06-10
8
2,315 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 100
Machine not responding during CopyFile() 3 98
Delphi XE10 Round Image 2 120
Error E2158 compiling with Delphi XE10 Seattle 2 98
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

816 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

10 Experts available now in Live!

Get 1:1 Help Now