locked
Find index node child in node parent (treeview) RRS feed

  • Question

  • hello !

    i have treeview follow

     

    A
    
    	a1
    
    	a2
    
    	a3
    
    B
    
    	b1
    
    	b2
    
    	b3
    
    

    now ! I want Get values node child in node parent ?

    ex :

    node child a1 in A

     

    but i dont know !

    plz help me ! tks !

     


    • Edited by NV Quyen Wednesday, October 26, 2011 6:32 PM
    Wednesday, October 26, 2011 6:31 PM

Answers

  • Not quite sure what you are asking but maybe the following will help.

    The TreeView has a nodes collection which comprises all the root nodes.

    Each root node also has a nodes collection as does each node in that collection and so on......

    To access any node in the tree you have to work down through all the parent nodes until you get to the one you require.

    Nodes in a collection can be accessed either by index or by name.

    So using your simple example tree:

          Dim Node1 As TreeNode = TreeView1.Nodes(0)
          MsgBox(Node1.Nodes(1).Name)
    
    
    will display A2

          Dim Node1 As TreeNode = TreeView1.Nodes("B")
          MsgBox(Node1.Nodes("B2").Index)
    
    

    will display 1

    Wednesday, October 26, 2011 9:25 PM
  • Hi NV Quyen,

              I do not know what value you Exactly.but I hope the below code will help you.

     


    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect Dim node As TreeNode = TreeView1.SelectedNode

    Dim index as integer = node.Index

     Dim parentnode As TreeNode = node.Parent MessageBox.Show("node child" & node.ToString() & " is in" & parentnode.Text ) End Sub

     

    If the above code helps you to solve your issues than pls Mark as Answer.



    Manikandan

    Friday, October 28, 2011 6:01 PM

All replies

  • Not quite sure what you are asking but maybe the following will help.

    The TreeView has a nodes collection which comprises all the root nodes.

    Each root node also has a nodes collection as does each node in that collection and so on......

    To access any node in the tree you have to work down through all the parent nodes until you get to the one you require.

    Nodes in a collection can be accessed either by index or by name.

    So using your simple example tree:

          Dim Node1 As TreeNode = TreeView1.Nodes(0)
          MsgBox(Node1.Nodes(1).Name)
    
    
    will display A2

          Dim Node1 As TreeNode = TreeView1.Nodes("B")
          MsgBox(Node1.Nodes("B2").Index)
    
    

    will display 1

    Wednesday, October 26, 2011 9:25 PM
  • Hi NV Quyen,

              I do not know what value you Exactly.but I hope the below code will help you.

     


    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect Dim node As TreeNode = TreeView1.SelectedNode

    Dim index as integer = node.Index

     Dim parentnode As TreeNode = node.Parent MessageBox.Show("node child" & node.ToString() & " is in" & parentnode.Text ) End Sub

     

    If the above code helps you to solve your issues than pls Mark as Answer.



    Manikandan

    Friday, October 28, 2011 6:01 PM