Loop through ALL Treeview nodes

M

Magus

I'm trying to list the process's of my computer into a treeview.
However I'm having a problem about looping through sub nodes.

I'm using the code:

TreeNodeCollection allnodes = treeView1.Nodes;
foreach (TreeNode n in allnodes)
{
if (ppid == Convert.ToString(n.Tag))
{
n.Nodes.Add(process);
}
}

The problem is that it only checks the root nodes. How do I solve this?
 
V

vanderghast

Use recursion?

void DoSomenthingOnNode( TreeNode n )
{

... // do something

foreach(TreeNode tn in n.Nodes)
{
DoSomethingOnNode(tn);
}
}


and launch the code from the root of the sub-tree you wish to visit.



Vanderghast, Access MVP
 
P

Peter Duniho

Magus said:
I'm trying to list the process's of my computer into a treeview.
However I'm having a problem about looping through sub nodes.

I'm using the code:

TreeNodeCollection allnodes = treeView1.Nodes;
foreach (TreeNode n in allnodes)
{
if (ppid == Convert.ToString(n.Tag))
{
n.Nodes.Add(process);
}
}

The problem is that it only checks the root nodes. How do I solve this?

You need to do it recursively:

void VisitNodes(TreeNodeCollection nodes, string ppid, object process)
{
foreach (TreeNode node in nodes)
{
if (ppid == Convert.ToString(node.Tag))
{
node.Nodes.Add(process);
}

VisitNodes(node.Nodes);
}
}

Or something like that. You weren't very specific about your declarations.

Pete
 
M

Magus

Thanks everyone. I don't have much experience with Treeviews or
recursion.

Here is the code I ended up using:

Go = true;
TreeNodeCollection allnodes = treeView1.Nodes;
foreach (TreeNode n in allnodes)
{
ParseNodes(n, process);
}

if (Go == true)
treeView1.Nodes.Add(process);


private void ParseNodes(TreeNode mynode, TreeNode process)
{
if (ppid == Convert.ToString(mynode.Tag))
{
mynode.Nodes.Add(process);
mynode.Expand();
Go = false;
}
foreach (TreeNode childnode in mynode.Nodes)
{
ParseNodes(childnode, process);
}
}

I added the Boolean because I couldn't figure out how to get it to add
to the root node without it.
 
P

Peter Duniho

Magus said:
[...]
I added the Boolean because I couldn't figure out how to get it to add
to the root node without it.

If you had used my suggestion, you would see that a TreeNodeCollection
is what's passed to the method, not an actual TreeNode. And of course,
if you pass a TreeNodeCollection, you can simply pass the root
collection as the initial collection.

Pete
 
M

Magus

Magus said:
[...]
I added the Boolean because I couldn't figure out how to get it to add
to the root node without it.

If you had used my suggestion, you would see that a TreeNodeCollection
is what's passed to the method, not an actual TreeNode.  And of course,
if you pass a TreeNodeCollection, you can simply pass the root
collection as the initial collection.

Pete

Hey Pete!

Thanks for your reply. I looked at your code soon after I "figured" it
out on my own. I thought it was generally the same but didn't see the
TreeNodeCollection. I actually switched to C# from Delphi so I'm still
pretty new to a few things. Thanks so much for pointing that out. It's
exactly what I was looking for. =]
 

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

Top