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...
Printable View
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...
Since there is no .Count property, you can do this:
VB Code:
Dim objNode As TreeNode Dim intCount as Integer TreeView1.Nodes.Add("A") TreeView1.Nodes.Add("B") intCount = 0 For Each objNode In TreeView1.Nodes intCount += 1 Next MsgBox(intCount)
Hope it helps,
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.
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.
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?
VB Code:
' give the index of the node ' True counts all levels of children ' False only counts one level deep in the children TreeView1.Nodes(0).GetNodeCount(True)