Alan T wrote:
I want to search the whole tree of a root node if one node has a tag value
is matched.
I have added tree nodes into a tree view with tag value is assigned to
each node.
If I want to find out the node has tag value say 10, how do I search thro'
the whole tree ?
Hi Alan,
You need a recursive algorithm:
///
private TreeNode FindNodeWithTag ( TreeNodeCollection nodes, object tag )
{
foreach ( TreeNode node in nodes )
{
if ( node.Tag == tag )
return node;
TreeNode candidate = FindNodeWithTag( node.Nodes, tag );
if ( candidate != null )
return candidate;
}
return null;
}
///
Then, you call it by passing in the collection of nodes from your treeview,
and the tag you want to search for. It will return the first node that
matches that tag.
--
Hope this helps,
Tom Spink