Search Treeview

G

Guest

Ok, I have a simple question but I don't think there is a simple answer.

I want to:
-search a Treeview node.text property for a value input by the user in a
textbox
-starting at the selectednode
-search all levels
-if the end of the tree is reached (and not found), start at the beginning
of the tree
-stop at the selected node if the text is not found in the tree

I did this is VB6 with no problem, but as we all know, the Treeview control
has changed dramatically in .net. I can search the treeview now (with
recursion) starting at the top and going through all nodes until found or at
the end. Which, by the way, helps to add nodes dynamically to a node that
already exists.

I'm hoping there is an easy way to do this without having to keep track of
which "level" your on and so forth. Any help is greatly appreciated.

Thanks,
Tom
 
G

Guest

Hi Tom,

Unfortunately I don't think you will find an easier solution to your problem
other than a simple recursive search through the nodes. As far as the Windows
Forms treeview control is concerned, there is a Nodes.Find method that
returns an array of matching treenodes.

The potential problem here is that the Find method works on a node's key and
not it's text value. keys have to be assigned when a node is added to it's
parent, e.g.

treeViewControl.Nodes.Add(keyName, textValue)

amongst a number of other related overloads for the Add method.

This of course means that you have to supply a predetermined node key when
building the tree to enable you to use the Find() method effectively. If you
have control over the node rendering process then perhaps this is an option.

Having a collection of matching nodes up-front as it were does give some
advantages depending on what you are specifically trying to do. For me it's
usually a case of a simple recursive method and keeping track of where I am.

Sorry I can't be of more help.

wibberlet
Development blog at http://wibberlet.blogspot.com
 

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