开发者

Is it possible to add values to tree nodes?

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

For example, a node may have the te开发者_如何学Pythonxt Desktop, but the value is C:\Documents and Settings\All Users\Desktop.


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; 


I know this is old post, but just in case anyone still wants the solution.

myTreeView.Nodes.Add("Key", "Text"); 

And you can access like

var key = myTreeView.SelectedNode.Name;
var text = myTreeView.SelectedNode.Text; 


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" };
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜