Please help with recursive count of childnodes in a TreeView
I'm trying to click on a node of a treeview and get a count of all nodes below it and throughout any sub levels of the hierachy.
I'm finding this difficult because of the way in which child nodes are 'modelled' with the TreeView component. Why each child is not an item of a children 'collection' escapes me!
Here I Give the Code To Finout the Childs For Selected Item
Dim i, j
Dim tempnod, Nod As Node
MsgBox TreeView1.SelectedItem.Children
j = TreeView1.SelectedItem.Children ' Set the First Child To Traverse
Set Nod = TreeView1.SelectedItem ' Set the Source Node To Find Childrens
If Nod.Children > 1 Then ' To Check Child is Avail
MsgBox Nod.Child
TreeView1.Nodes(Nod.Child.Next.Index).Selected = True ' Set The Next Child as Selected Node
End If
For i = 1 To j - 1 ' Loop Continue Untill The Last Child Reached
MsgBox TreeView1.SelectedItem
Set tempnod = TreeView1.SelectedItem ' Set the Selected as TempNode
If i < j - 1 Then
TreeView1.Nodes(tempnod.Next.Index).Selected = True ' Set The Next Child as Selected Node
End If
Next i
TreeView1.Nodes(Nod.Index).Selected = True ' Set the Source Node as Selected Node
If TreeView1.SelectedItem.EnsureVisible = False Then
TreeView1.SelectedItem.Expanded = True
End If
Put this Code Under The Fllowing For loop
Dim i As Node
For Each i In TreeView1.Nodes
Next
I Hope this Code Help To Findout The Children.
Regards,
Nambi
DavidRothan
ASKER
I have to award the points to angellll for such a rapid and concise solution to the question. The maddening thing is that angellls code was along the same lines as what I was trying to achieve except I had a loop:
set childnode = thenode.child
do until childnode=thenode.lastsibling
Here I Give the Code To Finout the Childs For Selected Item
Dim i, j
Dim tempnod, Nod As Node
MsgBox TreeView1.SelectedItem.Chi
j = TreeView1.SelectedItem.Chi
Set Nod = TreeView1.SelectedItem ' Set the Source Node To Find Childrens
If Nod.Children > 1 Then ' To Check Child is Avail
MsgBox Nod.Child
TreeView1.Nodes(Nod.Child.
End If
For i = 1 To j - 1 ' Loop Continue Untill The Last Child Reached
MsgBox TreeView1.SelectedItem
Set tempnod = TreeView1.SelectedItem ' Set the Selected as TempNode
If i < j - 1 Then
TreeView1.Nodes(tempnod.Ne
End If
Next i
TreeView1.Nodes(Nod.Index)
If TreeView1.SelectedItem.Ens
TreeView1.SelectedItem.Exp
End If
Put this Code Under The Fllowing For loop
Dim i As Node
For Each i In TreeView1.Nodes
Next
I Hope this Code Help To Findout The Children.
Regards,
Nambi