Home > Archive > Visual Basic Controls > January 2005 > HOWTO?: Treeview Child Node Count
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
HOWTO?: Treeview Child Node Count
|
|
| T Jones 2005-01-23, 9:00 pm |
| Hi,
Can someone let me know how to count the total number of Child Nodes
within a Treeview, without including Parent Nodes as well, please.
Eg:
Family (Parent Node - Don't want to include this in count)
Tom (Child Node - Only want Child Nodes in count)
Dick
Work (Parent Node - Don't want to include this in count)
Harry (Child Node - Only want Child Nodes in count)
Thank you.
Regards,
Tony
| |
|
| "T Jones" <hino5@hotmail.com> wrote in message
news:%23n$lMCe%23EHA.1296@TK2MSFTNGP10.phx.gbl...
> Hi,
>
> Can someone let me know how to count the total number of Child Nodes
> within a Treeview, without including Parent Nodes as well, please.
>
> Eg:
> Family (Parent Node - Don't want to include this in count)
> Tom (Child Node - Only want Child Nodes in count)
> Dick
> Work (Parent Node - Don't want to include this in count)
> Harry (Child Node - Only want Child Nodes in count)
>
> Thank you.
To my knowledge, you have to loop through nodes and increment a count of the
nodes that have a parent (or parent nodes that have children). This is
pretty simple as long as there's only one level of child nodes, as in your
example. Here's code for that:
Dim nodX As Node
Dim lChildNodeCount As Long
For Each nodX In TreeView1.Nodes
If Not nodX.Parent Is Nothing Then
lChildNodeCount = lChildNodeCount + 1
End If
Next
MsgBox "There are " & CStr(lChildNodeCount) & " child nodes."
You could also write it this way, but I don't know if there would be any
significant difference (I'm just showing this to demonstrate there are at
least 2 ways to do it):
Dim nodX As Node
Dim lChildNodeCount As Long
For Each nodX In TreeView1.Nodes
If nodX.Parent Is Nothing Then
lChildNodeCount = lChildNodeCount + nodX.Children
End If
Next
MsgBox "There are " & CStr(lChildNodeCount) & " child nodes."
For that matter, here's a third:
Dim nodX As Node
Dim lChildNodeCount As Long
Set nodX = TreeView1.Nodes(1)
Do While Not nodX Is Nothing
lChildNodeCount = lChildNodeCount + nodX.Children
Set nodX = nodX.Next
Loop
MsgBox "There are " & CStr(lChildNodeCount) & " child nodes."
This last one might be the best since it doesn't need to loop through all
the nodes. It just goes to the next sibling so there are fewer iterations of
the loop.
However, it could get a tad more complicated if you have something like this
because there are 2 levels of "parent" nodes:
Family
Jones
Tom
Smith
Dick
Work
IT
Harry
Sales
Shari
(Given the above tree, the first example would give a child node count of 8
while the 2nd and 3rd examples would give a child node count of 4)
I showed 3 different examples because from the 3 of them (and a little more
work on your own), you should be able to figure out how to get the child
node count for whatever needs you might have. You should also probably look
up other properties such as FirstSibling, LastSibling, Child, Previous, etc.
Any of these could be useful for your needs.
--
Mike
Microsoft MVP Visual Basic
| |
| Jeff Johnson [MVP: VB] 2005-01-23, 9:00 pm |
|
"T Jones" <hino5@hotmail.com> wrote in message
news:%23n$lMCe%23EHA.1296@TK2MSFTNGP10.phx.gbl...
> Can someone let me know how to count the total number of Child Nodes
> within a Treeview, without including Parent Nodes as well, please.
Dim nod As Node, ChildCount As Long
For Each nod In TreeView1.Nodes
If nod.Children = 0 Then
ChildCount = ChildCount + 1
End If
Next
| |
| Bob Butler 2005-01-23, 9:00 pm |
| "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:e6f2XEk%23EHA.2076@TK2MSFTNGP15.phx.gbl
> "T Jones" <hino5@hotmail.com> wrote in message
> news:%23n$lMCe%23EHA.1296@TK2MSFTNGP10.phx.gbl...
>
>
> Dim nod As Node, ChildCount As Long
>
> For Each nod In TreeView1.Nodes
> If nod.Children = 0 Then
> ChildCount = ChildCount + 1
> End If
> Next
ChildCount = TreeView1.Nodes.Count - ChildCount
<g>
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
| |
| Jeff Johnson [MVP: VB] 2005-01-23, 9:00 pm |
|
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:OOHk%23Zk%23EHA.608@TK2MSFTNGP15.phx.gbl...
>
> ChildCount = TreeView1.Nodes.Count - ChildCount
> <g>
??? Wouldn't that be the count of nodes that HAVE children, and not the
count of nodes that don't? Or did I completely screw up the code?
I interpreted the original poster's question thus: I need a count of leaf
nodes. So you increment ChildCount (or perhaps I should have called it
LeafCount) only when you find a node with no children.
| |
| Bob Butler 2005-01-23, 9:00 pm |
| "Jeff Johnson [MVP: VB]" <i.get@enough.spam> wrote in message
news:uenkYGl%23EHA.3416@TK2MSFTNGP09.phx.gbl
> "Bob Butler" <tiredofit@nospam.com> wrote in message
> news:OOHk%23Zk%23EHA.608@TK2MSFTNGP15.phx.gbl...
>
>
> ??? Wouldn't that be the count of nodes that HAVE children, and not
> the count of nodes that don't? Or did I completely screw up the code?
>
> I interpreted the original poster's question thus: I need a count of
> leaf nodes. So you increment ChildCount (or perhaps I should have
> called it LeafCount) only when you find a node with no children.
Ycode doesn't take into account the fact that there may be grandchildren and
would count any top-level nodes that happen to have no children.
I think the examples that counted when ".Parent Is Nothing" count more
appropriately
--
Reply to the group so all can participate
VB.Net: "Fool me once..."
| |
| Jeff Johnson [MVP: VB] 2005-01-23, 9:00 pm |
|
"Bob Butler" <tiredofit@nospam.com> wrote in message
news:eZZ8rLl%23EHA.2552@TK2MSFTNGP09.phx.gbl...
>
> Ycode doesn't take into account the fact that there may be grandchildren
How can a node have grandchildren if it doesn't have children?
> and would count any top-level nodes that happen to have no children.
Yes, I'm including root nodes, as I see them as children "of the tree
itself." Perhaps the original poster should have been more clear. I bet
Ralph is getting a chuckle out of yet another "I've Got a Secret" thread....
| |
|
| Thanks for all your answers, although I thought I was pretty clear. I am
only displaying only Parents and their Children. Just needed to know number
of Children. There are no Grandchildren.
Thanks again.
Regards,
TJ
|
|
|
|
|