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

随声附和 提交于 2020-01-13 02:30:07

问题


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.


回答1:


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




回答2:


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



回答3:


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.




回答4:


*Assumes empty TreeView:

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

rootNode.Nodes.Add("SubNode")


来源:https://stackoverflow.com/questions/9963870/vb-net-how-to-add-a-child-node-to-a-specific-node-in-treeview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!