TreeView.nodes.count diff in .Net ?

zekeman

Well-known member
Joined
May 23, 2006
Messages
224
Programming Experience
10+
I hope that I am not abusing this forum by asking particular code
questions like this. I should be asking questions about
using the new help system and I plan to move on to that after I
meet this one deadline. Please inform me of any wrong-doing.


In my VB6.0 app I could iterate all TV nodes using:
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] idX = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] TreeView1.Nodes.Count - 1 '( bounds adjusted for .net here)[/SIZE]
but now when I do it with VB.Net the count seems to only reflects the
count of the upper parent nodes ( no children included in count).

I have even tried the following but get the same results:
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myNodeCollection [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TreeNodeCollection = TreeView1.Nodes
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myCount [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = myNodeCollection.Count
MsgBox([/SIZE][SIZE=2][COLOR=#800000]"mycount is now: "[/COLOR][/SIZE][SIZE=2] & myCount)
[/SIZE]
Thanks
 
Last edited by a moderator:
Studying the help system I found the following:

For i = 0 To node.ChildNodes.Count - 1

which is hinting to me that I need to now count the childnodes.


The thing that is troubleing me is this:

Have they changed this count method and if so why doesn't the

.net express help inform us of that and why isn't there a conversion

comment next to the count code? Not that I'm complaining but I'm

just confused.

Thanks
 
I just need to be able to iterate through all the nodes of a treeview.

This is the old VB6.0 Code:


VB.NET:
Expand Collapse Copy
For idX = 0 To TreeView1.Nodes.Count - 1 '(bounds now adjusted for 2005) 

Tx = TreeView1.Nodes.Item(idX).Text
'Tx now contains The Text for this loops node and all nodes will be
' seen in this loop
 
...and so on



Thanks






 
Last edited by a moderator:
According to the help, this is the way to iterate a treeview. I have added
the button2_click. Does it need to be this difficult?

VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button2_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button2.Click
CallRecursive(TreeView1)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] PrintRecursive([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] n [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TreeNode)
System.Diagnostics.Debug.WriteLine(n.Text)
MessageBox.Show(n.Text)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] aNode [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TreeNode
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] aNode [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] n.Nodes
PrintRecursive(aNode)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE][SIZE=2][COLOR=#008000]' Call the procedure using the top nodes of the treeview.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] CallRecursive([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] aTreeView [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TreeView)
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] n [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TreeNode
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] n [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] aTreeView.Nodes
PrintRecursive(n)
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

[SIZE=2][COLOR=#0000ff]Thanks
[/COLOR][COLOR=black]
[/COLOR][/SIZE]
 
Last edited by a moderator:
First, doing a For Each iteration recursively isn't difficult. Second, you only need one method to recurse, example:

VB.NET:
Expand Collapse Copy
Private Sub DoAllNodes(ByVal nodes As TreeNodeCollection)
  For Each n As TreeNode In nodes
    MsgBox(n.Text)
    If n.Nodes.Count > 0 Then DoNodes(n.Nodes)
  Next
End Sub
Which you can call with this command:
VB.NET:
Expand Collapse Copy
DoAllNodes(TreeView1.Nodes)
Note that you don't have to check the child nodes count, but it will save one recursive call for each node without childs, a call that will immediately skip out of For-Each but nevertheless.. there will be more nodes without childs than with in normal cases, so doing so should save processing time.
 
Back
Top