在vb.net的文本框中显示树视图的所选节点的所有子节点

时间:2015-05-13 22:17:39

标签: vb.net treeview

不太确定如何解决这个问题。

我可以通过执行以下操作在文本框(或richtextbox)中显示当前所选节点:

Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect

    RichTextBox1.Text = e.Node.Text

End Sub

但是,我无法找到显示所选父级的所有子节点的方法。

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

您需要所谓的递归子例程来深入查看每个节点的子节点以迭代它们:

Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect

    Dim myNode As TreeNode = e.Node

    TextBox1.Text = myNode.Text

    GetChildNodes(myNode)

End Sub

Sub GetChildNodes(tnode As TreeNode)

    'Iterate through the child nodes of the node passed into this subroutine
    For Each node As TreeNode In tnode.Nodes

        TextBox1.Text += node.Text

        'If the node passed into this subroutine contains child nodes,
        'call the subroutine for each one of them
        'If you only want to display one level deep, then comment out the
        'IF statement.
        If tnode.Nodes.Count > 0 Then GetChildNodes(node)

    Next

End Sub