More Treeview questions

E

Elmo Watson

when I add nodes (child nodes) to a treeview:
..Nodes.Add(cdl.FileName, ShowingText)

the first item shows as the 'key', like it was in VB6 - - -

However, when I want to refer to that key, I can't find anything in
Intellisense for that

like :

txtPath.text=TV1.SelectedNode. - - here's where I would have thought there
would have been a 'key' in the Intellisense - but it isn't

so how is it done?
 
R

Robbe Morris [C# MVP]

People typically create a small custom struct or
class with the properties they want exposed and
place it in the .Tag property.

When they select a node, they get node,

CustomItem test = (CustomItem)tvTree.SelectedNode.Tag;

Debug.WriteLine(test.WhateverMyPropertyWas);

You can also do this with datarows and institute
a sort of databinding.

http://www.eggheadcafe.com/articles/treeview_databinding.asp
 
R

Rick Elbers

Two comments,

1) dont use c style casts if there is a better alternative.
The better alternative is to create some sort union using
implicit cast operators, then in the client use

CustomObject obj = tvTre.SelectedNode.Tag as CustomObject;
if (obj == null)
//handle programmer errors with at least an ASSERT.

2) Databinding of a tree is an absurd idea, which reminds me at
basic datastructure class. A tree is nothing but nodes like a
list. In fact the best bst tree implementation is a skip list:)

So, what does this mean ? You can't ever want to databind
a tree( which is nothing anyway) Its genericity stops when you have
nodes from more then one table(see:
http://www.eggheadcafe.com/articles/treeview_databinding.asp)

If you want to bind all nodes then the best approach seems
to use a prototype tree of different nodetypes and construct
new nodes from the prototype( see the pattern of the same name).
Identifie your prototypenodes with for instance an enum.

Then implement childdatabinding in the node. You can give the
node a table, tableadapter and adaptermethod and basically
you have databinding of the childnodes. Store the keyvalues in the
node. Add things for adaptermethods with parameters. Make selectors
from value( Where city = "Wageningen") or selectors from parent nodes
(Where city = @City), the last simply implemented by passing the
node's identifier level( see above)



Rick
 
E

Elmo Watson

Here's what I've found out
In VS.Net 2005 - if you give the node only one item, it's the Text property
of the node
If you feed it two items, the first one is the key.

Now - once you've given it the key, the Text property is just the visible
text
- there still is no 'key' property for nodes - - so how can I refer directly
to the key?
 
R

Robbe Morris [C# MVP]

We agree to disagree on that one. There are all sorts
of business cases for us where using my approach
works out great. It drastically improved our
code reusability in some areas.

That said, I recognize it is not a one size fits all
approach to managing UI to data functionality.
In 2005, generics have helped us in this area
as well.

--
Robbe Morris - 2004/2005 Microsoft MVP C#
http://www.eggheadcafe.com/forums/merit.asp
 

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