0

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.

  • Where is treeView1 declared? I suspect it's a private member of Form1 therefore not accessible to Form2. – HasaniH Apr 01 '22 at 18:55
  • What do you expect `SelectedItem` to be for a TreeView in a just-created invisible instance of `Form2` ? – Ben Voigt Apr 01 '22 at 19:05
  • @HasanHi, I just drag and drop the treeView1 in Form1 so I don't know treeView1 is private or public but **addToTreeView()** is public function. – dummy account Apr 02 '22 at 08:33

0 Answers0