Creating a new instance of Form1
will not work as it a new instance, not the one you were using. As the comments above suggest, the best option would be to create a custom constructor for Form2
and accept the textbox value there.
public Form2(string form1TextBoxValue)
{
this.ValueFromForm1 = form1TextBoxValue;
}
Therefore in Form1
you can use the value in Form2
when you instantiate it:
Form2 form2 = new Form2(this.textBox.Text);
Alternatively, you could pass the Form1
instance to Form2
. This way you can access all the Form1
values if needed. This does mean that Form2
has a direct dependence on Form1
which should be avoided.
// In Form2
public Form2(Form form1Instance)
{
this.Form1Instance = (Form1)form1Instance;
}
// In Form1
Form2 form2 = new Form2(this);