Solved

Treeview: How to count all children of a selected node

Posted on 2004-04-11
4
2,862 Views
Last Modified: 2008-03-17
Please give an example.

Probably very simple but i am not very familiar with the treeview.

In the TreeView_NodeClick function can someone give me an example how to count all the children of the node that is clicked ? Note: The node that is clicked will in certain cases will contain children with children with more children and all must be counted no matter how deep the sub directories of the node is.
0
Comment
Question by:brianshoenhair
4 Comments
 
LVL 1

Expert Comment

by:rminnich
ID: 10803752
Well, assuming vb6, you can use the Children property of the treeview to get the number of children of in the current node.  You can use the Nodes property to get the child Nodes of the current node.  Use recursion to iterate over all nodes to get the total.

The following link shows how to iterate over all the nodes using recursion although it doesn't count them.

http://www.vb-helper.com/howto_net_treeview_select_subtree.html
0
 
LVL 1

Expert Comment

by:pawankesari
ID: 10804090

TreeView1.SelectedItem.Children

You can replace TreeView1 with name of your TreeView Control.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 75 total points
ID: 10805086
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
    Label1.Caption = getNumChildren(Node)
End Sub

Private Function getNumChildren(ByVal Node As MSComctlLib.Node) As Integer
    Dim curNode As MSComctlLib.Node
    Dim numChildren As Integer
    Dim done As Boolean
   
    numChildren = Node.Children
    If numChildren > 0 Then
        done = False
        Set curNode = Node.Child
        Do
            If curNode.Children > 0 Then
                numChildren = numChildren + getNumChildren(curNode)
            End If
            If curNode.Key <> curNode.LastSibling.Key Then
                Set curNode = curNode.Next
            Else
                done = True
            End If
        Loop While Not done
    End If
    getNumChildren = numChildren
End Function
0
 

Author Comment

by:brianshoenhair
ID: 10810239
Thanks everyone for the help.

Idle_Mind, your example is exactly what I needed so you get the points. Thank You
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now