Calanus
Calanus

Reputation: 26317

How do I select a TreeNode by name?

The following code does not run as rootNode is null when retrieved by name "RootNode"

 treeView1.Nodes.Add(new TreeNode("RootNode"));

 ...
 //get the rootNode by its name
 TreeView1 rootNode = treeView1.Nodes["RootNode"]

 //rootNode is null so following line throws an error
 rootNode.Nodes.Add(new TreeNode("ChildNode"));

What am I missing here? How can I get a particular node by it's name??

Upvotes: 1

Views: 7082

Answers (1)

BlueMonkMN
BlueMonkMN

Reputation: 25601

The TreeNode constructor does not accept a key / name parameter. The indexer is based on the tree node's name, not its text. Therefore you either need to set the tree node's name or use a different add method like this:

treeView1.Nodes.Add("RootNode", "Root Node");

Upvotes: 7

Related Questions