473,407 Members | 2,315 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,407 software developers and data experts.

TreeNode location

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 TreeNode(textvalueofname)) it always
returns -1 and a TreeView.Nodes.Contains(etc.. always is false.
I expect that it is only looking in the local Nodes collection rather than
every single node in the TreeView.

So how do I easily find any given node in a TreeView if i only know the Text
field's value?

would i have to enumerate the entire collection?

TIA
Nev.
Nov 16 '05 #1
5 10204
Nev,

You will need to write a recursive routine to start at the
root node and chase down the tree until you find the node
you want.

Keep in mind that the resulting node's Index will be
relative only to its parent node...

John Bendiksen
-----Original Message-----
Hi,

If I have a TreeView with loads of Nodes and they all have children etc, howdo I find the index of a given node when I only know the Text value?If I use TreeView.Nodes.IndexOf(new TreeNode (textvalueofname)) it alwaysreturns -1 and a TreeView.Nodes.Contains(etc.. always is false.I expect that it is only looking in the local Nodes collection rather thanevery single node in the TreeView.

So how do I easily find any given node in a TreeView if i only know the Textfield's value?

would i have to enumerate the entire collection?

TIA
Nev.
.

Nov 16 '05 #2
No, you have to build recursive function to do it, but if you have someone
to peak it you can use
TreeNode tn = treeView.GetNodeAt(treeView.PointToClient(new Point(e.X,
e.Y)));
--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
"nevin" <nevin@[diespam]myself.com> wrote in message
news:40*********************@mercury.nildram.net.. .
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 TreeNode(textvalueofname)) it always
returns -1 and a TreeView.Nodes.Contains(etc.. always is false.
I expect that it is only looking in the local Nodes collection rather than
every single node in the TreeView.

So how do I easily find any given node in a TreeView if i only know the Text field's value?

would i have to enumerate the entire collection?

TIA
Nev.

Nov 16 '05 #3
Thanks Tamir and John,

I'm in the middle of my recursive routine already (untested as yet) as I
couldn't think of any other way to do it and to be honest I was hoping there
was an easier way (processing time)

Thanks for the replies.
"nevin" <nevin@[diespam]myself.com> wrote in message
news:40*********************@mercury.nildram.net.. .
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 TreeNode(textvalueofname)) it always
returns -1 and a TreeView.Nodes.Contains(etc.. always is false.
I expect that it is only looking in the local Nodes collection rather than
every single node in the TreeView.

So how do I easily find any given node in a TreeView if i only know the Text field's value?

would i have to enumerate the entire collection?

TIA
Nev.

Nov 16 '05 #4
I've done a lot of TreeView work and I'd like to offer up a couple of quick
notes that I've come across.

1. If you are re-expanding a tree to a specific node after performing some
operations on the tree, then save the tree-node that you will be expanding
to. Most users tend to update the tree, search for their node, and then expand
when they could just as easily save the node.

2. The TreeView is already a tree. Try to use directed recursive functions if
you do need to search. For instance, if you are searching for products then
they
are most likely categorized in some specific way. Use this to your advantage so
that you only recurse down the appropriate nested categories. You can even
save nodes that are used for frequent access. For instance, if the nodes are
wide
or deep, and the user is constantly performing searches of nodes within a
particular
parent, saving this parent node off helps me limit my search greatly.

3. Make use of custom nodes extensively. A custom node can perform a
registration
process that can make look-ups easier. The custom registration can be a
SortedList
or Hashtable that provides quick and easy look-ups. Making use of a custom
TreeView implementation allows you to store this information directly on the
TreeView
and each node can investigate their attached TreeView through the TreeView
property.

4. In place of custom nodes, use an adapter. Most people that make use of a
TreeView
also have the data in some other form. An adapter controls how nodes get added
to the
TreeView from some other data source. This could be an RSS feed, a DataSet, or
an
Xml dump from a SQL database. During the adapter process you can handle the
custom
registration in a centralized manner at initial start-up. The adapter can also
provide an
easy method for finding the parent control or defining a path to the node in
question
without a full recursive enumeration (see #2 on directed recursive functions).

5. There aren't any help functions that I know of that exist at the Win32 layer
for quickly
finding nodes. The Win32 layer relies on windows messages in order to obtain
information
about nodes and the find methods are only available through a hit-test method
that can
search by point and a get item method that relies on the native tree node
handle. You have
to roll your own search functions when it comes to the tree control.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"nevin" <nevin@[diespam]myself.com> wrote in message
news:40*********************@mercury.nildram.net.. .
Thanks Tamir and John,

I'm in the middle of my recursive routine already (untested as yet) as I
couldn't think of any other way to do it and to be honest I was hoping there
was an easier way (processing time)

Thanks for the replies.
"nevin" <nevin@[diespam]myself.com> wrote in message
news:40*********************@mercury.nildram.net.. .
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 TreeNode(textvalueofname)) it always
returns -1 and a TreeView.Nodes.Contains(etc.. always is false.
I expect that it is only looking in the local Nodes collection rather than
every single node in the TreeView.

So how do I easily find any given node in a TreeView if i only know the

Text
field's value?

would i have to enumerate the entire collection?

TIA
Nev.


Nov 16 '05 #5
There is no direct way to find the node in c#.

In case of huge clustered trees, its really important to find the node as
quick as possible. Finding node after recursing will be waste of time.

Here is what i recommend.

While constructing the tree nodes, a XML document has in the same fashion.

for example - Lets say we have one root node and 2 children.

//init treeview and xml
TreeView myTree = new TreeView();
XmlDocument myDoc = new XMLDocument();

//construct rootnode
TreeNode pRootNode = new TreeNode("Root");

//add rootnode to treeview.
TreeView.Nodes.Add(pRootNode);
//add 2 childs
pRootNode.Nodes.Add("Child1")
pRootNode.Nodes.Add("Child2");

For the above hierarchy, XmlDocument has to be constructed same way.

After construction of TreeView, XML is ready for quering and its going to be
pretty fast in searching nodes. To find a node programatically XmlDocument
has to searched using GetElementsByTagName() for example

This approach will save time when the tree is too deep, like MSDN tree found
in http://msdn.microsoft.com

--
Shak
(Houston)

"nevin" <nevin@[diespam]myself.com> wrote in message
news:40*********************@mercury.nildram.net.. .
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 TreeNode(textvalueofname)) it always
returns -1 and a TreeView.Nodes.Contains(etc.. always is false.
I expect that it is only looking in the local Nodes collection rather than
every single node in the TreeView.

So how do I easily find any given node in a TreeView if i only know the Text field's value?

would i have to enumerate the entire collection?

TIA
Nev.


--
Shak
(Houston)


"nevin" <nevin@[diespam]myself.com> wrote in message
news:40*********************@mercury.nildram.net.. . 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 TreeNode(textvalueofname)) it always
returns -1 and a TreeView.Nodes.Contains(etc.. always is false.
I expect that it is only looking in the local Nodes collection rather than
every single node in the TreeView.

So how do I easily find any given node in a TreeView if i only know the Text field's value?

would i have to enumerate the entire collection?

TIA
Nev.

Nov 16 '05 #6

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

Similar topics

3
by: scoobydoo | last post by:
Hello, I am trying to implement ICloneable's Clone() function, using Serialization. However, my code causes an exception. I have a class derived from TreeNode called "Node1". In Node1, I...
2
by: Benny Raymond | last post by:
More problems with this... When I run this code, the main form returns an invalid cast exception as it's executing the line "TreeNode n = (TreeNode) this.Nodes;" Does anyone know what would...
0
by: naija naija | last post by:
Hi All, I'm using the TreView Web Control for my MENU. And the Menu is populated from an XML file below. I want a way in which i can manipulate the XML when i want to manage it without having to...
3
by: markaelkins | last post by:
Hi. I am trying to enter a variable in the treenodesrc of a treenode. I am basically trying to send an ID variable into sql to return different records. I've searched everywhere and cannot find the...
0
by: fred | last post by:
I am using the clipboard to copy and paste a TreeNode but the Tag property does not seem to be copied. I use the Tag property for an Object that contains data relating to the TreeNode. The Tag...
0
by: Sagaert Johan | last post by:
Hi How can i create a treeNode that contains also a Combobox. I Added a combobox to the treeview controls collection, but when i scroll the treeview, the combobox stays at its location. I...
0
by: divya1949 | last post by:
Create a windows c# application which will Read a xml file and populate nodes in the treeview. 1 On selection of treenode display the child nodes of that node in listview control 2. ...
1
by: jmDesktop | last post by:
I am trying to add nodes with keys to my treeview. I can add general nodes without problem with: //create new node TreeNode newNode = new TreeNode(myIdNumber); //create children TreeNode...
1
by: AAaron123 | last post by:
If you see this posted twice - sorry. My news reader showed my first post as "No Longer Available" I have the following in a .css file. The treeNodes behave as if they were "a" elements. ...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.