VB.NET 如何将子节点添加到树视图中的特定节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9963870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VB.NET How to add a child node to a specific node in treeview
提问by NetInfo
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?
如何将子节点添加到树视图中的特定节点?假设我已经在树视图中有“Item1”,如何将“SubItem1”添加到“Item1”作为它的子节点?
I know its probably really simple, but i tried lots of stuff, i just cant get it working.
我知道它可能真的很简单,但我尝试了很多东西,我就是无法让它工作。
回答by msigman
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.
首先用于Find()
获取对父节点的引用。然后使用与下面其他部分相同的技术添加它。
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.
如果要将子节点添加到特定的父节点,想法是使用parent.node.add()
方法将子节点添加到其父节点。您可以像这样创建任意数量的孩子。
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
http://www.codeproject.com/Articles/11830/The-Basic-Operations-on-using-the-TreeView-Control
回答by Mark Hall
If you make sure that you assign a Name
to your TreeNode
You can use Find
to locate it and add the Child
node.
如果您确保将 a 分配Name
给您的TreeNode
您可以使用Find
它来定位它并添加Child
节点。
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
回答by Gaandi
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
所以我到了这个页面: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")
事实证明,我们只需要继续输入 nodes.add("nodename") 即可继续添加子级别。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
http://img716.imageshack.us/img716/7254/semttulonzk.jpg
Hope it Helped ;D.
希望它有所帮助;D。
回答by David Osborne
*Assumes empty TreeView:
*假设空树视图:
Dim rootNode = TreeView1.Nodes.Add("Root")
rootNode.Nodes.Add("SubNode")