views:

81

answers:

2

Is it possible to add text, and a value, to a tree node?

For example, a node may have the text Desktop, but the value is C:\Documents and Settings\All Users\Desktop.

+1  A: 

A TreeNode has a Tag property. You can set that to any object you like and can use as your underlying value, while the normal text of the node is displayed in the tree.

E.g.

TreeNode node = new TreeNode("Desktop") { Tag = "C:\Documents and Settings\All Users\Desktop" };
Ian
+1  A: 

You may to use Tag property for storing values:

TreeNode node = new Node();
node.Tag = "value";

Advantage is that you can assign to Tag any object you want not just integer or string value.

Then you can use Tag as follow:

var value = node.Tag as YourObjectType; 
sashaeve
Thank you so much. I only need to use a single string value. I was just about to write a Dictionary<String, String>, and go through the hassle of checking if the boxes are checked and getting the values from the Dictionary. Saved me a little time.
James Jeffery
@James Jeffery : Out of curiosity what was wrong with my answer posted first?
Ian
@Ian nothing bud, sash provided a bit more info, but your information was just the same. If I could tick both boxes I would. When I refreshed the page there were 2 answers. Nothing personal :)
James Jeffery
@James. Ok fair enough. Newest normally get shoved to the top automatically :(
Ian