1

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!

0

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

Comments

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.