Results 1 to 8 of 8

Thread: [RESOLVED] Get all parents from a selected node

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Location
    Earth, I think!!
    Posts
    249

    Resolved [RESOLVED] Get all parents from a selected node

    I got a treeview with several levels (about 1500 nodes). How can i get in a listbox all the parent nodes of the selected node?

  2. #2
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,254

    Re: Get all parents from a selected node

    A node only has one parent. Please clarify your question.
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Location
    Earth, I think!!
    Posts
    249

    Re: Get all parents from a selected node

    Yes I know, but the nodes parent got a parent ... let me be more clear....
    for example...

    root
    .....Parent A
    ................SubParent 1
    ................................node 1
    ................................node 2
    ................SubParent 2
    ................................node 3
    .....Parent B
    ................node 4

    if the selected is the "node 3" in the list should in the Subparent 2,Parent A,Root
    if the selected is the "node 4" in the list should in the Parent B,Root

  4. #4
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,254

    Re: Get all parents from a selected node

    OK, much clearer. You want all ancestors of a particular node.

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim i As Long, j As Long
       For i = 1 To 50
          TreeView1.Nodes.Add , tvwChild, "Node" & i, "Node " & i
       Next i
    
       On Error Resume Next
       
       Randomize
       For i = 1 To 50
          
          j = Int((50 * Rnd) + 1)
          Set TreeView1.Nodes(i).Parent = TreeView1.Nodes(j)
       Next i
    
    End Sub
    
    
    Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    
       List1.Clear
       List1.AddItem Node.Text
       Do
          If Not Node.Parent Is Nothing Then
             Set Node = Node.Parent
             List1.AddItem Node.Text
          Else
             Exit Do
          End If
       Loop Until False
    
    End Sub
    Last edited by ColinE66; Jun 19th, 2014 at 05:22 AM.
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  5. #5
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Get all parents from a selected node

    I would write the loop differently but yes you would have to loop until there is not a parent node found.
    Code:
    Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
        
        List1.Clear
        List1.AddItem Node.Text
        
        Do While Not Node.Parent Is Nothing
            Set Node = Node.Parent
            List1.AddItem Node.Text
        Loop
    End Sub

  6. #6
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,254

    Re: Get all parents from a selected node

    Yup. Although I never use While, personally. Probably would have done it as below if I'd thought about it rather than bashing it out in a few seconds (slightly more readable IMO)...

    Code:
       List1.Clear
       List1.AddItem Node.Text
       Do Until Node.Parent Is Nothing
          Set Node = Node.Parent
          List1.AddItem Node.Text
       Loop
    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Location
    Earth, I think!!
    Posts
    249

    Re: Get all parents from a selected node

    works fine thanks !!!

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: [RESOLVED] Get all parents from a selected node

    Just in case you are not really interested in the Ancestors String-Representations in a List(Box),
    but want to use them for "Path-Like" purposes, there's the 'FullPath' property on any Node,
    which might be helpful too.

    Below is a small example, which is somewhat "misusing" Node.FullPath, to accomplish
    what you wanted - here only serving as a reminder, that the Texts of the ancestor-
    nodes are already available in a "nicely arranged way" in the Node itself (ready to be
    directly used in simple String-Ops).

    Code:
    Option Explicit
    
    Private Sub Form_Load()
      With TreeView1.Nodes
        .Add , , "A", "A"
          .Add "A", tvwChild, "Aa", "Aa"
            .Add "Aa", tvwChild, "Aaa", "Aaa"
      End With
    
      AddAncestorsFromNode TreeView1.Nodes("Aaa")
    End Sub
    
    Private Sub AddAncestorsFromNode(ByVal Node)
      List1.Clear
      For Each Node In Split(Node.FullPath, "\")
        List1.Additem Node 'alternatively List1.AddItem Node, 0 for reverse ordering
      Next
    End Sub
    Olaf

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width