Solved

Treeview: How to count all children of a selected node

Posted on 2004-04-11
4
2,780 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…

708 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

11 Experts available now in Live!

Get 1:1 Help Now