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?
Printable View
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?
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
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
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
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 ;)