Results 1 to 9 of 9

Thread: [RESOLVED] A specific treeview branch

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] A specific treeview branch

    I have this treeview with 5 node levels. At program start after it has been populated, I want all nodes collapsed except the first node of level 3 and all its children and grandchildren nodes. How do I refer to and loop over these?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: A specific treeview branch

    There is Expanded property:
    Code:
    Private Sub Form_Load()
        With TreeView1
            .Style = tvwTreelinesPlusMinusPictureText
            .Nodes.Add , , "KR", "Root"
            .Nodes.Add "KR", tvwChild, "KC1", "Child 1"
            .Nodes.Add "KC1", tvwChild, "KGC11", "Grand Child 1"
            .Nodes.Add "KC1", tvwChild, "KGC12", "Grand Child 2"
            .Nodes.Add "KC1", tvwChild, "KGC13", "Grand Child 3"
            .Nodes.Add "KR", tvwChild, "KC2", "Child 2"
            .Nodes.Add "KC2", tvwChild, "KGC21", "Grand Child 1"
            .Nodes.Add "KC2", tvwChild, "KGC22", "Grand Child 2"
            .Nodes.Add "KC2", tvwChild, "KGC23", "Grand Child 3"
            .Nodes("KR").Expanded = True
            .Nodes("KC1").Expanded = True
        End With
    End Sub

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: A specific treeview branch

    Quote Originally Posted by RhinoBull View Post
    There is Expanded property...
    Maybe I was a bit confusing.
    Expanded is not the question but how to cycle through all the children and grand children of a specific node without using their keys. Is there no way to refer to the children nodes as a collection?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: A specific treeview branch

    Aha... Here is another quicky and I'm sure you'll be able to figure out what it does.
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim i%, j%
    
        With TreeView1
            .Style = tvwTreelinesPlusMinusPictureText
            .Nodes.Add , , "KR", "Root"
            For i = 1 To 2
                .Nodes.Add "KR", tvwChild, "KC" & i, "Child " & i
                For j = 1 To 3
                    .Nodes.Add "KC" & i, tvwChild, "KGC" & i & j, "Grand Child " & i & "-" & j
                Next j
            Next i
            .Nodes("KR").Expanded = True
            .Nodes("KC1").Expanded = True
        End With
    
    End Sub
    
    Private Sub Command1_Click()
    Dim nd As MSComctlLib.Node
    
        For Each nd In TreeView1.Nodes
            If nd.Children Then
                IterateNodes nd
            End If
        Next nd
    
    End Sub
    
    Private Sub IterateNodes(ByVal Node As MSComctlLib.Node)
    Dim nd As MSComctlLib.Node
    Dim i As Long
    
        Set nd = Node.Child
        For i = 1 To Node.Children
            Debug.Print nd.Text
            Set nd = nd.Next
        Next i
    
    End Sub

  5. #5

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: A specific treeview branch

    Quote Originally Posted by RhinoBull View Post
    Aha... Here is another quicky and I'm sure you'll be able to figure out what it does.
    ...
    This:

    For i = 1 To Node.Children

    was the important point I was missing, I wasn't aware that Node.Children was a counter of all child nodes.

    Thanks RB
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: [RESOLVED] A specific treeview branch

    There is something interesting about Treeviews that might be useful for you: Since you have that many levels, you could also take advantage of iteration just inside a given level and work with all the nodes in that branch even when you don't know how many levels down or nodes need to be iterated. Each node has parent (just 1) and Children (1 to many), and you can also move inside a given level using Node.Next and/or Node.Previous when iterating. This could be useful in cases like this since you need to iterate just level 3 nodes + all sublevels/nodes in that branch, not all the nodes in the treeview, to do such task you need recursion, here i'll show you an example using Rhino's Form_Load() code to fill the TV with 5 levels, also added a Commandbutton, when clicking it you're calling 2 subs: ExpandParents will expand this node's parents (they need to be expanded if you want the children to be visible) and the second sub is ExpandChildren, this one iterates throught the node's Children expanding them recursively.
    Code:
    Private Sub Form_Load()
    Dim i%, j%, x%, z%
    
        With TreeView1
            .Style = tvwTreelinesPlusMinusPictureText
            .Nodes.Add , , "Level1", "Root"
            For i = 1 To 2
                .Nodes.Add "Level1", tvwChild, "Level2" & i, "Level2 " & i
                For j = 1 To 3
                    .Nodes.Add "Level2" & i, tvwChild, "Level3" & i & j, "Level3" & i & j
                    For x = 1 To 3
                        .Nodes.Add "Level3" & i & j, tvwChild, "Level4" & i & j & x, "Level4" & i & j & x
                        For z = 1 To 3
                            .Nodes.Add "Level4" & i & j & x, tvwChild, "Level5" & i & j & x & z, "Level5" & i & j & x & z
                        Next
                    Next
                Next
            Next
        End With
    End Sub
    
    Private Sub Command1_Click()
        ExpandParents TreeView1.Nodes("Level311"), True  'Expand this node's parents with Recursion
        ExpandChildren TreeView1.Nodes("Level311"), True 'Expand this node's Children with Recursion
    End Sub
    
    
    Private Sub ExpandChildren(ByVal pNode As MSComctlLib.Node, pExpand As Boolean)
    Dim oChildNode As Node
        
        pNode.Expanded = pExpand
        Set oChildNode = pNode.Child
        Do Until oChildNode Is Nothing
            oChildNode.Expanded = pExpand
            ExpandChildren oChildNode, pExpand 'Recursive Call
            Set oChildNode = oChildNode.Next
        Loop
    End Sub
    
    Private Sub ExpandParents(ByVal pNode As MSComctlLib.Node, pExpand As Boolean)
        Set pNode = pNode.Parent
        If Not pNode Is Nothing Then
            pNode.Expanded = pExpand
            ExpandParents pNode, pExpand
        End If
    End Sub
    Last edited by jcis; Jul 9th, 2010 at 06:02 AM.

  7. #7

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] A specific treeview branch

    Quote Originally Posted by jcis View Post
    ...Since you have that many levels, you could also take advantage of iteration...
    Very nice code, I think recursive functions are very elegant -though more difficult to code, you often end up overflowing the stack if not careful...
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: [RESOLVED] A specific treeview branch

    You're right about that, the exit condition must be clear when using recursion, in this case i ask if (Node is Nothing) and this is the exit condition.

    PS: España merece ganar la final, suerte para este domingo! Holanda será incluso más fácil que Alemania

  9. #9

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: [RESOLVED] A specific treeview branch

    Quote Originally Posted by jcis View Post
    ...PS: España merece ganar la final, suerte para este domingo! Holanda será incluso más fácil que Alemania
    Yo que soy muy viejo recuerdo la final del 74 que debia ganar Holanda, pero gano Alemania segun el dicho de Gary Lineker... espero que no ocurra algo parecido, gracias!
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

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