Treenode collection (easy!)

  • Thread starter Thread starter Bob Hollness
  • Start date Start date
B

Bob Hollness

Hi all. I know this is easy. What have I missed? I want to iterate
through all child nodes from the top.

Dim treeVal as TreeNode = Nothing

For Each treeVal in MyTree.Nodes
if treeVal.Checked = True then MsgBox(treeVal.Text)
Next
 
Bob said:
Hi all. I know this is easy. What have I missed? I want to iterate
through all child nodes from the top.

Dim treeVal as TreeNode = Nothing

For Each treeVal in MyTree.Nodes
if treeVal.Checked = True then MsgBox(treeVal.Text)
Next

AFAIK, this should work. However, i only get a message if i tick the
topmost node. it still does not loop through all the childs

Any ideas?
 
MyTree.Nodes only contains top level nodes. Each node under that also
has a Nodes property that returns it's child nodes, and so on.

Use a recursive function such as:

Sub LookForChecked(nodes as TreeNodeCollection)

for each node as TreeNode in nodes
if node.Checked then MessageBox.Show(node.Text)
LookForChecked(node.Nodes)
next

end sub

LookForChecked(MyTree.Nodes)


HTH,

Sam
 
Samuel said:
MyTree.Nodes only contains top level nodes. Each node under that also
has a Nodes property that returns it's child nodes, and so on.

Use a recursive function such as:

Sub LookForChecked(nodes as TreeNodeCollection)

for each node as TreeNode in nodes
if node.Checked then MessageBox.Show(node.Text)
LookForChecked(node.Nodes)
next

end sub

LookForChecked(MyTree.Nodes)


HTH,

Sam
Thanks! I'll give this a try.

One thing that threw me (and maybe you could explain it??) is that I used
the After_Click event to perform an action. One of the values returned is
e as TreeviewArguments (or something like that). When I do a For Each on
e.Nodes it iterates every sub node perfectly. So what is the difference?
 
TreeViewEventArgs has a Node (singular) property that is the node that
was clicked. If you iterate it's children as in

for each nod as TreeNode in e.Node.Nodes
....
next

Then it will iterate the child of that node--direct children only and
not grandchildren.

If the tree has just two levels, clicking on the one root node will
make it appear that all nodes are iterated in the event handler.

Really you still need the recursive loop to get all desendent nodes.

HTH,

Sam
One thing that threw me (and maybe you could explain it??) is that I used
the After_Click event to perform an action. One of the values returned is
e as TreeviewArguments (or something like that). When I do a For Each on
e.Nodes it iterates every sub node perfectly. So what is the difference?

Assume you mean AfterCheck event...
 
Samuel said:
TreeViewEventArgs has a Node (singular) property that is the node that
was clicked. If you iterate it's children as in

for each nod as TreeNode in e.Node.Nodes
...
next

Then it will iterate the child of that node--direct children only and
not grandchildren.

If the tree has just two levels, clicking on the one root node will
make it appear that all nodes are iterated in the event handler.

Really you still need the recursive loop to get all desendent nodes.

HTH,

Sam

Yes, I meant AfterCheck.

1 problem. Mine is iterating everything. I tested at least 5 levels down
and it works. Are you sure that it won't iterate grand-children? Because
my testing shows that it goes down to at least the
great-great-great-grandchild. ?????

The code i used is this:

Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e as
System.Windows.Forms.TreeViewEventArgs)

Dim Child as TreeNode = Nothing

For Each Child In e.Node.Nodes

If e.Node.Checked = True Then
Child.Checked = True
Else
Child.Checked = False
End If

Next

End Sub
 
Bob said:
Hi all. I know this is easy. What have I missed? I want to iterate
through all child nodes from the top.

Dim treeVal as TreeNode = Nothing

For Each treeVal in MyTree.Nodes
if treeVal.Checked = True then MsgBox(treeVal.Text)
Next
FYI - thanks for a the help. I now have it working!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top