I created two forms one for getting the value and in other for assigning the value in treeView in the second form. When I pass the value in the textbox which contains the treeView (both are in the same Form) it works but when I passes the value from the second Form it gives the mentioned error.
1st Form
public void addToTreeView()
{
string store = _name + " - " + _role + "( S$" + _salary.ToString() + ") ";
MessageBox.Show(store);
treeView1.SelectedNode.Nodes.Add(store); //error line
name.Add(_name); //list
role.Add(_role); //list
salary.Add(_salary); //list
}
2nd Form
private void button1_Click(object sender, EventArgs e)
{
string store = comboxObj.SelectedItem.ToString();
Form2 obj = new Form2(textBox3.Text.ToString() , store , int.Parse(textBox4.Text)); //parameterized constructor
obj.addToTreeView();
this.Close();
}
In this I am getting the value from the Form2 into Form1 treeView. I even created the parameterized function addToTreeView() but it still gives the error. I used message box to check whether I am getting the value from the other Form it is even getting the correct value from the Form2.