473,413 Members | 1,737 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,413 software developers and data experts.

TreeNodes search

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 ?
Jul 7 '06 #1
4 12001
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 ?
The simplest idea is to search the tree recursively:

private TreeNode SearchNodes(TreeNodeCollection nodes)
{
TreeNode result = null;

foreach(TreeNode node in nodes)
{
// Here check the search condition.
// Sample:
if(node.Tag.ToString() == "10")
{
result = node;
}
else
{
result = SearchNodes(node.Nodes);
}

if(result != null)
{
break;
}
}

return result;
}

private TreeNode SearchTreeView(TreeView view)
{
return SearchNodes(view.Nodes);
}

Best regards!
Marcin
Jul 7 '06 #2
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
Jul 7 '06 #3
Hi I got an exception here:

private TreeNode SearchNode(TreeNodeCollection aNodes, int aId)

{

TreeNode result = null;
foreach ( TreeNode node in aNodes )

{

if (node.Tag.ToString() == aId.ToString()) <--- exception

{

return node;

}

The exception is "The object reference is not set to an instance of object"
The simplest idea is to search the tree recursively:

private TreeNode SearchNodes(TreeNodeCollection nodes)
{
TreeNode result = null;

foreach(TreeNode node in nodes)
{
// Here check the search condition.
// Sample:
if(node.Tag.ToString() == "10")
{
result = node;
}
else
{
result = SearchNodes(node.Nodes);
}

if(result != null)
{
break;
}
}

return result;
}

private TreeNode SearchTreeView(TreeView view)
{
return SearchNodes(view.Nodes);
}

Best regards!
Marcin

Jul 10 '06 #4
Alan T wrote:
if (node.Tag.ToString() == aId.ToString()) <--- exception

The exception is "The object reference is not set to an instance of object"
Apparently your nodes have the Tag property set to null. I assumed that
you use it to store some information. If you store information in the
Tag property of only some of your nodes, just check it before the
comparison:

if(node.Tag != null)
{
if(node.Tag.ToString() == aId.ToString())
{
return node;
}
}

Best regards!
Marcin
Jul 10 '06 #5

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

Similar topics

0
by: R. Rajesh Jeba Anbiah | last post by:
Q: Is PHP search engine friendly? Q: Will search engine spiders crawl my PHP pages? A: Spiders should crawl anything provided they're accessible. Since, nowadays most of the websites are been...
28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
0
by: Norbert Heidbüchel | last post by:
Hi all, I have read a lot of postings and web pages about drag and drop and treeviews, but could not find an answer to my problem. Sorry, if I missed something. I am trying to drag and drop...
3
by: Kilroy Programmer | last post by:
Is there a way to store a unique numeric identifier (say, for example, an int) into a TreeNode, so that when the TreeNode is checked (since CheckBoxes is enabled) the eventhandler AfterCheck() can...
10
by: DrPete | last post by:
Hi People Could anyone suggest to me the best way to distinguish treenodes? By this I mean I'm in a situation where I have a TreeNode that displays a number of categories and within those nodes...
0
by: David | last post by:
Can WebControls be added to TreeNodes in the TreeView? David -- Be Positive Every Day
0
by: Josema | last post by:
Hi, Im making an asyncronous application that will detect broken links inside a web site. the question is that i have a tree view in my windows form application, and when a person enters a...
0
by: SBelzile | last post by:
Hi, We are working on a vb.net WinForms project based on the Composite UI Application Block (VS2005). I have a strange problem. At first, I thought 10 minutes would be fairly enough for me to...
2
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
My first time using a TreeView: I've got a TreeView that I populated with some data and it has nodes. In the Properties window, I created an Event Handler for the TreeView's AfterSelect...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.