How to add a child node to a specific node in treeview? Say I have "Item1" in treeview already, how do I add "SubItem1" to "Item1" as it's child node?
I know its probably really simple, but i tried lots of stuff, i just cant get it working.
TreeView enables you to edit nodes in applications, you need to set the AllowEditing property of the C1TreeView class to true. The default value of the property is false. You can start editing a node by selecting a node and pressing the Enter or F2 key, or simply double-clicking the node itself.
A typical tree structure has only one root node; however, you can add multiple root nodes to the TreeView control. The Nodes property can also be used to manage the root nodes in the tree programmatically. You can add, insert, remove, and retrieve TreeNode objects from the collection.
First use Find()
to get a reference to the parent node. Then add it using the same technique as the other sections below.
Dim MyNode() As TreeNode
MyNode = TreeView1.Nodes.Find("Item1", True)
MyNode(0).Nodes.Add("SubItem1")
If you want to add the child nodes to a particluar parent node, the idea is to add the child nodes to their parent node by using the parent.node.add()
method.
You can create any number of child like this.
For example if you want to have a scenario like:
Grandfather-> Father-> Son
Then you could do this:
dim GrandfatherNOde as treenode = tree.nodes.add("Grandfather")
dim fatherNode as treenode = GrandfatherNode.Nodes.add("Father")
dim sonNode as treenode = fatherNode.Nodes.add("Son")
This page has a good example you can run to dynamically add child nodes to the tree. They do it on a button, which they've hooked up like this:
Private Sub AddChild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddChild.Click
TView.SelectedNode.Nodes.Add(Text1.Text)
End Sub
http://www.codeproject.com/Articles/11830/The-Basic-Operations-on-using-the-TreeView-Control
If you make sure that you assign a Name
to your TreeNode
You can use Find
to locate it and add the Child
node.
Example:
Public Class Form1
Dim Nodes(5) As TreeNode
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Nodes(0) = New TreeNode("Root")
Nodes(0).Name = "Root"
Nodes(1) = New TreeNode("Item1")
Nodes(1).Name = "Item1"
Nodes(2) = New TreeNode("Item2")
Nodes(2).Name = "Item2"
Nodes(3) = New TreeNode("Item3")
Nodes(3).Name = "Item3"
Nodes(4) = New TreeNode("Item4")
Nodes(4).Name = "Item4"
Nodes(0).Nodes.Add(Nodes(1))
Nodes(0).Nodes.Add(Nodes(2))
Nodes(0).Nodes.Add(Nodes(3))
Nodes(0).Nodes.Add(Nodes(4))
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TreeView1.Nodes.Add(Nodes(0))
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim tmpNode() As TreeNode = TreeView1.Nodes.Find("Item1", True)
'Assuming only one Match
tmpNode(0).Nodes.Add("Child Of Item1")
End Sub
End Class
I was looking for the same thing when i got here, and so far i couldnt get to what i needed.
So i got to this page: http://www.dotnetspider.com/forum/168335-How-add-node-treeview-VB.NET.aspx
Really cool and simple to do after you give it a look.
It turn out that we only need to keep typing nodes.add("nodename") to keep adding sublevels. Treeview1.Nodes.Add("Root_1").Nodes.Add("Child_Level_1").Nodes.Add("Child_Level_2")
Treeview1.Nodes.Add("Root_1").Nodes.Add("Child_Level_1").Nodes.Add("Child_Level_2")
This would get something like:
http://img716.imageshack.us/img716/7254/semttulonzk.jpg
Hope it Helped ;D.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With