Results 1 to 6 of 6

Thread: Counting child nodes in a treeview

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    113

    Counting child nodes in a treeview

    I have a treeview that is populated when the my form loads. Now I want to be able to count the child nodes for any parent node that a user selects.

    Is their an easy way to do get the # of children ?

    Thanks...

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Since there is no .Count property, you can do this:

    VB Code:
    1. Dim objNode As TreeNode
    2.         Dim intCount as Integer
    3.  
    4.         TreeView1.Nodes.Add("A")
    5.         TreeView1.Nodes.Add("B")
    6.         intCount = 0
    7.         For Each objNode In TreeView1.Nodes
    8.             intCount += 1
    9.         Next
    10.         MsgBox(intCount)

    Hope it helps,

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2002
    Posts
    113
    Thanks for your reply.....


    Yes that will count the nodes but I need to count ONLY the child nodes of the parent that the user selects.


    eg.


    Customer <--- currently selected parent w/ 3 children
    Order
    Order
    Order
    Customer
    Customer
    Customer

    If I can get the count of the currently selected parent's children I want to use that count to display in the status bar the number of orders for that customer. However, my problem is ... how can I tell the level of the node and only count the children of that selected parent ?


    Thanks so very much for any help.

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    I dont have .Net in front of me right now, but the nodes in a treeview are recursive, so every node has a .Nodes property that can be looped through. All you have to do is find the selected node and loop through its .Nodes property.

  5. #5
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    couldn't you check against the index? if it's zero, then it's a parent, right?

    if treeview1.nodes...index > 0 then
    'count them

    something like that?

  6. #6
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    VB Code:
    1. ' give the index of the node
    2. ' True counts all levels of children
    3. ' False only counts one level deep in the children
    4.  
    5. TreeView1.Nodes(0).GetNodeCount(True)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width