使用TreeView在vb.net中为子节点创建子节点

时间:2014-12-04 20:10:26

标签: xml vb.net treenode

我正在尝试以编程方式将子节点添加到我的TreeView中。

最初我是使用TreeView1.Nodes(0).Nodes(0).Nodes(0)手动完成的... 这自然开始看起来很讨厌,但当我尝试将记录附加到记录时也不会起作用。

我当前的代码将把我所有的代码放在根级别,因为使用TreeView1.Nodes.Add(tn)的函数在技术上是根代码。如何在不需要手动编码的情况下将其设置为动态(避免TreeView1.Nodes(0).Nodes(0).Nodes(0) ...)

订单是:

Main
 |_ Screen
     |_ Msg
     |_ A
     |_ B
     |_ C
     |_ D

VB.net

    add_child_node(doc.DocumentElement.SelectSingleNode("/Main").Name)

    Private Sub add_child_node(NodeName As String)
        Dim tn As New TreeNode(NodeName)
        'TreeView1.SelectedNode.Nodes.Add(tn)
        TreeView1.Nodes.Add(tn)
    End Sub

XML

    <Main>  
      <Screen ID="0" ParentID="-1">
        <Msg></Msg>
        <A ChildID="1"></A>
        <B ChildID=""></B>
        <C ChildID=""></C>
        <D ChildID=""></D>
      </Screen>
    </Main>

1 个答案:

答案 0 :(得分:0)

TreeNode:它是如何工作的?

Dim ScreenNode As TreeNode
Dim MyChildNode As TreeNode = TreeView1.Nodes.Add("<Main>")

    'TreeView
    ' |_ <Main> <- MyChildNode points to this Node

MyChildNode = MyChildNode.Nodes.Add("<Screen>")
ScreenNode = MyChildNode

    'TreeView
    ' |_ <Main>
    '     |_ <Screen> <- MyChildNode and ScreenNode both are pointing to this Node

MyChildNode = MyChildNode.Parent

    'TreeView
    ' |_ <Main> <- MyChildNode points to this Node
    '     |_ <Screen> <- ScreenNode points to this Node

MyChildNode = MyChildNode.Nodes.Add("<Screen2>")

    'TreeView
    ' |_ <Main>
    '     |_ <Screen> <- ScreenNode points to this Node
    '     |_ <Screen2> <- MyChildNode points to this Node

MyChildNode = TreeView1.Nodes.Add("<Main2>")

    'TreeView
    ' |_ <Main>
    ' |   |_ <Screen> <- ScreenNode points to this Node
    ' |   |_ <Screen2>
    ' |_ <Main2> <- MyChildNode points to this Node

Dim CurrentNode As TreeNode
For i As Int32 = 1 To 5
    CurrentNode = MyChildNode.Nodes.Add(i.ToString())
Next
MyChildNode = CurrentNode

    'TreeView
    ' |_ <Main>
    ' |   |_ <Screen> <- ScreenNode points to this Node
    ' |   |_ <Screen2>
    ' |_ <Main2>
    '     |_ <1>
    '     |_ <2>
    '     |_ <3>
    '     |_ <4>
    '     |_ <5> <- MyChildNode points to this Node

ScreenNode.Nodes.Add("<Msg>")

    'TreeView
    ' |_ <Main>
    ' |   |_ <Screen> <- ScreenNode points to this Node
    ' |       |_ <Msg>
    ' |   |_ <Screen2>
    ' |_ <Main2>
    '     |_ <1>
    '     |_ <2>
    '     |_ <3>
    '     |_ <4>
    '     |_ <5> <- MyChildNode points to this Node

递归!

Private Function BuildTreeOf(ByRef CurrentXMLNode As SomeXMLNode) As TreeNode
    Dim CurrentTreeNode As New TreeNode(CurrentXMLNode.Name)

    For Each ChildXML As SomeXMLNode In CurrentXMLNode.Children
        CurrentTreeNode.Add(BuildTreeOf(ChildXML))
    Next

    Return CurrentTreeNode
End Function

' Call :

TreeView1.BeginUpdate()
TreeView1.Nodes.Add(BuildTreeOf(tn))
' Where tn is the topmost parent of your XML data as SomeXMLNode.
TreeView1.EndUpdate()

我把一个SomeXMLNode类型的占位符对象放在一个.Children属性中,它是一个(排序的)同类型对象的集合,因为我使用自己的XML Parser而且我已经失去了对原生dotNet的跟踪关于XML的对象。尝试使用您正在操作的XML对象来调整上述内容。