Please help with recursive count of childnodes in a TreeView

Posted on 2003-02-28
Medium Priority
Last Modified: 2007-12-19
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!
Question by:DavidRothan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 200 total points
ID: 8041585
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


Expert Comment

ID: 8041775

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.



Author Comment

ID: 8041937
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question