Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Treeview: How to count all children of a selected node

Posted on 2004-04-11
4
Medium Priority
?
3,589 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 86

Accepted Solution

by:
Mike Tomlinson earned 300 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

972 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