Avatar of DavidRothan
DavidRothan
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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!
Visual Basic Classic

Avatar of undefined
Last Comment
DavidRothan

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Guy Hengel [angelIII / a3]

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sazhagianambi

Hi,

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

Your help has saved me hundreds of hours of internet surfing.
fblack61