Solved

Treeview: How to count all children of a selected node

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Add a task in Outlook from access 11 42
Visual Studio 2005 text editor 10 44
Problem to With line 4 63
Problem to line 23 53
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

839 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