|
-
Jul 8th, 2010, 11:20 AM
#1
[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)
-
Jul 8th, 2010, 11:53 AM
#2
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
-
Jul 8th, 2010, 01:51 PM
#3
Re: A specific treeview branch
 Originally Posted by RhinoBull
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)
-
Jul 8th, 2010, 03:38 PM
#4
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
-
Jul 9th, 2010, 04:17 AM
#5
Re: A specific treeview branch
 Originally Posted by RhinoBull
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)
-
Jul 9th, 2010, 05:34 AM
#6
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.
-
Jul 9th, 2010, 07:27 AM
#7
Re: [RESOLVED] A specific treeview branch
 Originally Posted by jcis
...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)
-
Jul 9th, 2010, 07:36 AM
#8
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
-
Jul 9th, 2010, 08:53 AM
#9
Re: [RESOLVED] A specific treeview branch
 Originally Posted by jcis
...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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|