Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Treeview: How to count all children of a selected node

Posted on 2004-04-11
4
Medium Priority
?
3,431 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
[X]
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
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
Suggested Courses

721 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