PC Review


Reply
Thread Tools Rate Thread

Treenode collection (easy!)

 
 
Bob Hollness
Guest
Posts: n/a
 
      25th Jan 2005
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


--

I'll have a B please Bob.
 
Reply With Quote
 
 
 
 
Bob Hollness
Guest
Posts: n/a
 
      25th Jan 2005
Bob Hollness wrote:

> 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?

--

I'll have a B please Bob.
 
Reply With Quote
 
 
 
 
rawCoder
Guest
Posts: n/a
 
      25th Jan 2005
U need to do it recursively. Incase there are nested nodes.

Here's a link for what you wanna do
http://msdn.microsoft.com/library/de...iewcontrol.asp

HTH
rawCoder


"Bob Hollness" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
> --
>
> I'll have a B please Bob.



 
Reply With Quote
 
Samuel R. Neff
Guest
Posts: n/a
 
      25th Jan 2005

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



On Tue, 25 Jan 2005 17:53:49 +0100, Bob Hollness <(E-Mail Removed)>
wrote:

>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


 
Reply With Quote
 
Bob Hollness
Guest
Posts: n/a
 
      25th Jan 2005
Samuel R. Neff wrote:

>
> 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
>
>
>
> On Tue, 25 Jan 2005 17:53:49 +0100, Bob Hollness <(E-Mail Removed)>
> wrote:
>
>>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

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?

--

I'll have a B please Bob.
 
Reply With Quote
 
Samuel R. Neff
Guest
Posts: n/a
 
      25th Jan 2005

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...

 
Reply With Quote
 
Bob Hollness
Guest
Posts: n/a
 
      26th Jan 2005
Samuel R. Neff wrote:

>
> 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

--

I'll have a B please Bob.
 
Reply With Quote
 
Bob Hollness
Guest
Posts: n/a
 
      26th Jan 2005
Bob Hollness wrote:

> 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!
--

I'll have a B please Bob.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft ASP .NET 1 18th May 2007 10:24 AM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Øyvind Isaksen Microsoft Dot NET 1 18th May 2007 10:24 AM
what is equivalent of vb.net treenode.nextnode property in asp.net webcontrols.treenode tanya foster Microsoft ASP .NET 3 18th May 2006 06:58 PM
TreeNode Collection juli jul Microsoft C# .NET 7 4th Apr 2005 10:58 AM
TreeNode collection without user data Ondrej Sevecek Microsoft Dot NET Framework Forms 1 23rd Oct 2003 02:20 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:13 PM.