Treeview and keys. Please help; I've been frustrated on this one for a long time

G

Guest

There has been a lot of discussion lately about extending the treeview control to allow finding a node by key rather than by index. Everyone seems to agree that a hashtable is needed to accomplish this. Also, there is a KB article (http://support.microsoft.com/default.aspx?scid=kb;en-us;311318) purporting to show you how to extend the treenode to include a key. This article seems to be nearly useless to me, as (despite adding a DictionaryEntry type property to the treenode) it ends up using a looping function in the extended treeview's CLIENT code to find the node.

Again, it seems everyone agrees that a hashtable is needed. But I don't understand how to do it. I would THINK that I need to override the TreeNodeCollection's Add method to not only add the node to the collection, but also add a (key, index) pair to a hashtable-type property. But I can't seem to do this because replacing a treeview's TreeNodeCollection with an extended TreeNodeCollection requires inheriting the TreeNodeCollection and then calling its New(Owner as TreeNode) sub, which is private.

Is there something I'm missing here? Since I've clearly been unable to understand how to implement the solution that appears to be clear to others, please include an explicit example.

Thanks a lot.

Pat
 
R

Robin Tucker

Basically, what you are missing I think is that in general I and perhaps
others don't bother deriving from TreeNodeCollection. Its rather more
complex and convoluted than what you need.

Instead, what I do is place a tree view "dock filled" on a form, then in the
code section I add a hashtable, followed by "Add", "Remove", "GetNode"
methods to the form. Any clients of the tree have to execute methods
through the form object, not actually on the treeview itself (although I
guess you could expose the TreeView control itself from the form).

pmcguire67 said:
There has been a lot of discussion lately about extending the treeview
control to allow finding a node by key rather than by index. Everyone seems
to agree that a hashtable is needed to accomplish this. Also, there is a KB
article (http://support.microsoft.com/default.aspx?scid=kb;en-us;311318)
purporting to show you how to extend the treenode to include a key. This
article seems to be nearly useless to me, as (despite adding a
DictionaryEntry type property to the treenode) it ends up using a looping
function in the extended treeview's CLIENT code to find the node.
Again, it seems everyone agrees that a hashtable is needed. But I don't
understand how to do it. I would THINK that I need to override the
TreeNodeCollection's Add method to not only add the node to the collection,
but also add a (key, index) pair to a hashtable-type property. But I can't
seem to do this because replacing a treeview's TreeNodeCollection with an
extended TreeNodeCollection requires inheriting the TreeNodeCollection and
then calling its New(Owner as TreeNode) sub, which is private.
Is there something I'm missing here? Since I've clearly been unable to
understand how to implement the solution that appears to be clear to others,
please include an explicit example.
 
A

AlexS

I think the easiest way for you would be to add methods like
AddNode/RemoveNode/LookupNode to treeview and replace standard calls
treeview.Nodes.Add/Remove/RemoveAt n your code with new ones. New methods
will update / search your hashtable in addition to Nodes updates. Not 100%
foolproof, but should alleviate your woes when implemented carefully.
I don't see now the way how to extend TreeNodeCollection for standard
TreeView. Maybe somebody else knows?

HTH
Alex

pmcguire67 said:
There has been a lot of discussion lately about extending the treeview
control to allow finding a node by key rather than by index. Everyone seems
to agree that a hashtable is needed to accomplish this. Also, there is a KB
article (http://support.microsoft.com/default.aspx?scid=kb;en-us;311318)
purporting to show you how to extend the treenode to include a key. This
article seems to be nearly useless to me, as (despite adding a
DictionaryEntry type property to the treenode) it ends up using a looping
function in the extended treeview's CLIENT code to find the node.
Again, it seems everyone agrees that a hashtable is needed. But I don't
understand how to do it. I would THINK that I need to override the
TreeNodeCollection's Add method to not only add the node to the collection,
but also add a (key, index) pair to a hashtable-type property. But I can't
seem to do this because replacing a treeview's TreeNodeCollection with an
extended TreeNodeCollection requires inheriting the TreeNodeCollection and
then calling its New(Owner as TreeNode) sub, which is private.
Is there something I'm missing here? Since I've clearly been unable to
understand how to implement the solution that appears to be clear to others,
please include an explicit example.
 

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