.NET 2.0 TreeView Control - Need help with Key

G

Guest

Hi everyone.... I am writing a new Windows app using VS 2005.

My memory must be failing me, but I was under the impression that when I
call the "nodes.add" methods and I supply a key property, that that value
must be unique. I was then able to make reference to that node later on my
using that key.

I did some testing, and it looks like I can give all of the nodes in the
tree the same key.

So, is there something new that I'm missing? I need to expand a node based
on a unique value (I am using GUID.tostring right now). To keep it simple,
let say I have 26 nodes, and randomly label them A to Z. If I want to
programatically expand node M, (without interaction with the treeview
control), how can I do that? Using the "Index" value won't do the trick, as
I would never know where it would appear in the treeview. I also don't want
to loop through all nodes, as there are times when I may have a few thousand
of them.

Thanks,

Forch
 
C

Claes Bergefall

How about keeping a reference to every node in a hashtable (or a generic
Dictionary even)? That way you can quickly look it up using a key and then
expand it (or whatever you want to do with it).

I don't think that using the new "key" feature of the treeview/treenode is
very efficient. The key is simply the Name property of the TreeNode. Guess
it's up to you to ensure that it is unique. You can then use the Find method
to find it, but that will just do a standard recursive search (i.e. the loop
you didn't want) and the comparison is not case sensitive.

/claes
 
S

Stoitcho Goutsev \(100\)

Forch,

No, the key doesn't have to be unique. The docs doesn't spell out this, but
giving the fact that one can find in the docs that the IndexOfKey method
retruns the first occurance of a node with specified key or that the Find
method returns array of nodes having specified key is a good evidence that
node keys are not expected to be unique.

I don't know why they call it *key* since this is simply the *Name* of the
tree node. Probably they wanted to avoid the confusion wehere the
programmers will assume that the name has to be unique, but it looks like
they chose wrong name again.

Anyways, if you want to force uniqueness of keys/names you need to do it by
yourself, by keeping all used keys in some sort of collection.
 

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