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.