473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12020
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(Tre eNodeCollection nodes)
{
TreeNode result = null;

foreach(TreeNod e node in nodes)
{
// Here check the search condition.
// Sample:
if(node.Tag.ToS tring() == "10")
{
result = node;
}
else
{
result = SearchNodes(nod e.Nodes);
}

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

return result;
}

private TreeNode SearchTreeView( TreeView view)
{
return SearchNodes(vie w.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 ( TreeNodeCollect ion 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(Tree NodeCollection aNodes, int aId)

{

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

{

if (node.Tag.ToStr ing() == 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(Tre eNodeCollection nodes)
{
TreeNode result = null;

foreach(TreeNod e node in nodes)
{
// Here check the search condition.
// Sample:
if(node.Tag.ToS tring() == "10")
{
result = node;
}
else
{
result = SearchNodes(nod e.Nodes);
}

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

return result;
}

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

Best regards!
Marcin

Jul 10 '06 #4
Alan T wrote:
if (node.Tag.ToStr ing() == 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.ToS tring() == 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
4163
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 developed with PHP, you are not supposed to doubt that. As a proof that PHP pages could be crawled and indexed, refer this Google search
28
3184
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 linear search the best here(for loop from beginning to end of array)? It seems like some other search algorithm like binary or whatever would be of no benefit on this data set.
0
2423
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 treenodes defined by myself and don't understand, how to get the nodes data in the dragdrop event. I'm not very experienced in .NET and for sure there is a simple solution. I've written a short sample program, based on well known examples,
3
2711
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 examine the responsible Node's identifier to see which TreeNode triggered the event? Analyzing the Node's Text string is undesirable because it would mean performing a string compare to a set of predefined strings. This is slower and not easily...
10
1763
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 there are items of stock. There are a number of things I wish to do, such as use a ContextMenu to Rename the node, but I can't seemingly tell if the Node I'm renaming is a category or an item of stock.
0
1120
by: David | last post by:
Can WebControls be added to TreeNodes in the TreeView? David -- Be Positive Every Day
0
1240
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 url and click in the button "search broken links" the application starts visit this page, get the html content and then try to visit each link inside this content and again the same, until the complete web site been analized...
0
1228
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 solve it but I've spent almost 6 hours on it today and still can't figure out what's wrong. Using the CAB, I have built a control that intercepts messages coming from other CAB's SmartParts controls and display them into a treeview control. The...
2
2192
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 method, and it allows me to determine what Tree Node has been selected. However, if I expand the TreeView and select one of the Child TreeNodes, they do not trigger this event.
0
9715
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9175
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7642
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.