AlimMurat
AlimMurat

Reputation: 11

Adding a new root node to a existing treeview's parent node

I selected the parent node (the top) in treeview. and now i need to add a new parent node to the existing parent node dynamicly. here is my way of solving to the problem: `

treeView1.SelectedNode.Parent.Nodes.Add(textBox1.Text.Trim());
//here comes an error of null reference argument

//To avoid that error,i tried it in this way:
   if (treeView1.SelectedNode.Parent == null)
        treeView1.SelectedNode.Parent=new TreeNode(textBox1.Text.Trim());

` But it still returns an error. Help me solve this. Thanks guys!

Upvotes: 1

Views: 852

Answers (1)

MrApnea
MrApnea

Reputation: 1946

Trying this wont work:

treeView1.SelectedNode.Parent.Nodes.Add(textBox1.Text.Trim());

Since you are trying to find the Parent of the top node who doesn't have a top node.

To add a new top node you need to add it to the list of top nodes:

treeView1.Nodes.Add(textBox1.Text.Trim());

Upvotes: 1

Related Questions