473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Treenode collection (easy!)

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.
Nov 21 '05 #1
7 4932
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.
Nov 21 '05 #2

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 <bo*@blockbuster.com>
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


Nov 21 '05 #3
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 <bo*@blockbuster.com>
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.
Nov 21 '05 #4

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

Nov 21 '05 #5
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" <bo*@blockbuster.com> wrote in message
news:On**************@TK2MSFTNGP14.phx.gbl...
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.

Nov 21 '05 #6
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.
Nov 21 '05 #7
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.
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Brian | last post by:
I'm reasonably new to C#, and using a class containing a hashtable collection to feed a treeview. My problem is that I can't pull the data from the hashtable! Here's the code I have: if...
5
by: nevin | last post by:
Hi, If I have a TreeView with loads of Nodes and they all have children etc, how do I find the index of a given node when I only know the Text value? If I use TreeView.Nodes.IndexOf(new...
7
by: juli jul | last post by:
Hello, I am trying to create a TreeNodeCollection and to attach it to a tree view. How exactly can I do it? I am trying to do: TreeNodeCollection tnc=new TreeNodeCollection(node); but keep...
0
by: Ferrari.NET | last post by:
Hi all I have a treeview in a form that I need to store inside a nodes collection so I'll not loose the values when I change between screens. What I'm doing is store all the checked nodes into...
0
by: Patrick.O.Ige | last post by:
I have this code below. All its suppose to do is to EXPAND / COLLAPSE ALL of my treeview. But when i use ASP.NET WEbmatrix it all works fine .. But with VS.NET is says TreeNodeCollection not...
6
by: Jan Krouwer | last post by:
I have a treeview which is populated from a relational database. In order to copy part of the tree, I need to add to the database the relationship of the part of the tree to be copied but with new...
3
by: Locke Nash Cole | last post by:
I've made my first collection. Its a simple one but it works. Now I want to populate a treeview from my collection to display to the user, easy enough... But now comes my dilemma.. say I add a...
8
by: | last post by:
I have the following class : Public Class MyTreeNode Inherits TreeNode Dim mystring As String End Class Now, when I try to do this : ''''''''''''nodes is a TreeNodeCollection, s is string
3
by: tanya foster | last post by:
Hello, I am re-writing a visual basic .net application(visual studio 2003) in an asp.net application(visual studio 2005). The vb.net application relied on a treeview and hence, treenodes. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.