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!

Juliet
  • 80,494
  • 45
  • 196
  • 228
Spectraljump
  • 4,189
  • 10
  • 40
  • 55
  • 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. – VVS Dec 01 '10 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. – Spectraljump Dec 01 '10 at 21:50

4 Answers4

8

You can use Insert instead of Add.

tree.Nodes[item.name].Nodes.Insert(2, (new TreeNode("thing")));
Davita
  • 8,928
  • 14
  • 67
  • 119
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

Ran
  • 5,989
  • 1
  • 24
  • 26
  • 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. – Spectraljump Dec 01 '10 at 21:48
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);
IAbstract
  • 19,551
  • 15
  • 98
  • 146
  • Regarding your Edit, I won't be having a "SelectedNode" to add to. So that method (of which I read btw) doesn't help.. – Spectraljump Dec 01 '10 at 21:35
  • 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. :) – IAbstract Dec 01 '10 at 21:38
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"));

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 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.. – Spectraljump Dec 01 '10 at 21:40