Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 573
  • Last Modified:

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!
1 Solution
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this (recursive) function should help you:

Public Function ChildCount( byval n as Node ) as Long
  dim c as Node
  dim lngResult as Long
  set c = n.Child
  while not (c is nothing)
    lngResult = lngResult + 1 + ChildCount (c)
    set c = c.Next

  ChildCount = lngResult
End Function


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

I Hope this Code Help To Findout The Children.


DavidRothanAuthor Commented:
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


Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now