Reputation: 599
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.
Upvotes: 1
Views: 5395
Reputation: 13313
Try this :
if(carTree.SelectedNode == null)
MessageBox.Show("Please select a node first");
carTree.SelectedNode.Nodes.Add(new TreeNode("Child"));
Upvotes: 2