如何获取treeview子节点的索引?

时间:2014-02-14 10:25:52

标签: vb.net winforms

如何获取父节点的子节点的索引?我的树视图是这样的:

  • 根节点1
    • 节点1
    • 节点2
  • 根节点2
    • 节点1
    • 节点2
  • 根节点3
    • 节点1
    • 节点2

例如,我想获取Root节点2的子节点索引,我该怎么做?

'Imports System.Windows.Forms

Private Sub trv1_AfterSelect(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles trv1.AfterSelect
    Dim a As Integer
    x = e.Node.Index
    debug.print x
End Sub

1 个答案:

答案 0 :(得分:0)

好的我已经完成了我的工作而不是获取所选父节点的子节点的索引我正在反向执行它将立即获得所选子节点的父索引。并从下面阐述代码

Private Sub treeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles treeView1.AfterSelect

If (e.Node.Parent IsNot Nothing) 
  If (e.Node.Parent.GetType() Is GetType(TreeNode)) Then
     label1.Text = "Parent: " + e.Node.Parent.Text + _
       ControlChars.Cr + "Parent Index: " + e.Node.Parent.Index.ToString()
  End If 
Else
  label1.Text = "No parent node." 
End If 

End Sub