1

I have a treeview that contains Car Makes and their respective Models. I have a button that adds a new Make (parent Node).

private void btnMake_Click(object sender, EventArgs e)
{
    string inputMake;
    inputMake = Microsoft.VisualBasic.Interaction.InputBox("Enter Make: ", "Add Car Manufacturer");
    carMake.Add(inputMake); // arrayList to store car Makes
    carTree.Nodes.Add(new TreeNode(inputMake));
}

What I am having problems with is adding Model (child nodes). I have a button to add model and I don't know how to differentiate the appropriate parent node.

I currently have the following code:

private void btnModel_Click(object sender, EventArgs e)
{
    string inputModel;
    int index = carTree.Nodes.IndexOf(carTree.SelectedNode);
    //MessageBox.Show(carMake[index].ToString());
    //inputModel = Microsoft.VisualBasic.Interaction.InputBox("asfdasdf", "asdfasdf");
    //carTree.Nodes[index].Nodes.Add(new TreeNode(inputModel));
}

The last lines are commented out due to testing . . . I am putting the care Makes (parent nodes) into an ArrayList but am having issues accessing the arraylist. This line returns an error:

 //MessageBox.Show(carMake[index].ToString());

Ultimately I would like some help with the most efficient way to add child nodes to a respective parent node.

2
  • How does it work ? Will it add child nodes to the currently highlighted node ? Where and how is the parent node choosen ? Commented Apr 3, 2013 at 19:43
  • its a windows form app. so the parent node would be chosen by selecting and then clicking the add child button. i also have to figure out an exception message if no parent node has been selected. Commented Apr 3, 2013 at 19:50

1 Answer 1

2

Try this :

if(carTree.SelectedNode == null)
  MessageBox.Show("Please select a node first");    

carTree.SelectedNode.Nodes.Add(new TreeNode("Child"));
Sign up to request clarification or add additional context in comments.

1 Comment

No problem. Don't forget to mark this question as answered if your problem is solved.

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.