VB.NET How to add a child node to a specific node in treeview
Asked Answered
M

4

6

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.

Microphone answered 1/4, 2012 at 11:4 Comment(0)
R
11

Adding child node to parent (non-selected)

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")

Adding nodes programmatically

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")

More reading/examples

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

Rebuttal answered 1/4, 2012 at 11:9 Comment(4)
Thanks for your quick reply, but this doesn't seem to be working.Microphone
I've included a more complete example. Let me know if it works for you. If not, we'll figure out what the problem is from there.Rebuttal
Thanks a bunch for all these examples, but again the "Grandfather" is already added. what i want to do is add the "Father" to the "Grandfather" without selecting it. I already know how to add all three at the same time. Sorry for sounding like a noob. thanks again.Microphone
No problem, I understand what you're saying. Let me update my post to be more clear.Rebuttal
J
3

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
Juvenescent answered 1/4, 2012 at 16:51 Comment(0)
M
2

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.

Marlite answered 18/5, 2013 at 16:59 Comment(0)
M
1

*Assumes empty TreeView:

Dim rootNode = TreeView1.Nodes.Add("Root")

rootNode.Nodes.Add("SubNode")
Moonmoonbeam answered 1/4, 2012 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.