Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Adding nodes to a Treeview at runtime

Posted on 2001-06-10
8
Medium Priority
?
2,824 Views
Last Modified: 2011-10-03
How can I use 'Add' or AddChild to a selected TreeNode at runtime?
0
Comment
Question by:henrylim
[X]
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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 60 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

609 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