5

How does one insert a new child to a particular node in a TreeView in C# WinForms?

I've been clumsily stabbing at TreeViews for almost an hour and I'd like to use C#'s TreeView like this:

treeView.getChildByName("bob").AddChild(new Node("bob's dog"));

Here's what I tried last (which I think is at a level of hairiness which C# should never have allowed me to reach):

tree.Nodes[item.name].Nodes.Add(new TreeNode("thing"));

Needless to say, it doesn't work.

Oh, and here's a lazy question: can you actually store objects in these nodes? Or does TreeNode only support strings and whatnot? (in which case I should extend TreeNode.. /sigh)

Please help, thanks!

2
  • If you're free to choose, take a look at WPF. You can model your data as you like and bind it to the TreeView. Commented Dec 1, 2010 at 21:23
  • Oh, dude, I would switch to WPF in a heartbeat if I were to have the option.. For starters, anything with the help of XML is a step forward. Commented Dec 1, 2010 at 21:50

4 Answers 4

8

You can use Insert instead of Add.

tree.Nodes[item.name].Nodes.Insert(2, (new TreeNode("thing")));
Sign up to request clarification or add additional context in comments.

Comments

6

Actually your code should work - in order to add a sub node you just have to do:

myNode.Nodes.Add(new TreeNode("Sub node"));

Maybe the problem is in the way you refer to your existing nodes. I am guessing that tree.Nodes[item.Name] returned null?

In order for this indexer to find the node, you need to specify a key when you add the node. Did you specify the node name as a key? For example, the following code works for me:

treeView1.Nodes.Add("key", "root");
treeView1.Nodes["key"].Nodes.Add(new TreeNode("Sub node"));

If my answer doesn't work, can you add more details on what does happen? Did you get some exception or did simply nothing happen?

PS: in order to store an object in a node, instead of using the Tag property, you can also derive your own class from TreeNode and store anything in it. If you're developing a library, this is more useful because you are leaving the Tag property for your users to use.

Ran

1 Comment

Thank you, sir! tree.Nodes[item.Name] was indeed returning null (though it took me some time to figure it out). I did not know how to set an index key like you showed. This was my problem;(I also had an "unrelated" issue that confused me further) but I couldn't fetch the node where I wanted to insert. Thanks again.
3

Well, to start out, yes you can store objects in each node. Each node has a Tag property of type object.

Adding nodes should be fairly straightforward. According to MSDN:

// Adds new node as a child node of the currently selected node.
TreeNode newNode = new TreeNode("Text for new node");
treeView1.SelectedNode.Nodes.Add(newNode);

2 Comments

Regarding your Edit, I won't be having a "SelectedNode" to add to. So that method (of which I read btw) doesn't help..
Ah...it looks like Ran has a few good examples and appear as though they would work - no need for me to rehash what he has. :)
0

Otherwise if Davita's isn't the perfect answer, you need to retain a reference to the nodes, so if you had a reference to bob you could add bob's dog

TreeNode bob= new TreeNode("bob"); treeView1.Nodes.Add(bob); bob.Nodes.Add(new TreeNode("Dog"));

1 Comment

It's true that I need some sort of reference. But I can't do what you wrote since I won't be adding nodes in the same function where I'm inserting. Regardless, Ran has solved my problem and enlightened me: I didn't have a "key", and I didn't know how to set one..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.