TreeNode location

N

nevin

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.
 
J

John Bendiksen

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
 
N

nevin

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.
 
T

Tamir Khason

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)));
 
J

Justin Rogers

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.
 
S

Shakir Hussain

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 said:
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.
 

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