Odd questions among 2 forms

J

Jason Huang

Hi,

This has bothered me for a while.
In Form1:
private void btnToForm2_Click(object sender, System.EventArgs e)
{
Form2 MyForm2=new Form2();
MyForm2.Show();
MyForm2.textBox1.Text="open Form 2 ";
}
If I close the Form1 at this moment, the program will be closed, since the
Form1 is the starting form.

In Form2:
private void btnToForm1_Click(object sender, System.EventArgs e)
{
Form1 MyForm1=new Form1();
MyForm1.Show();
MyForm1.textBox1.Text="open Form 1 ";
this.Close();
}

My question is when I was in Form2, how can I return to the original Form1
and change the value of the Form1.textBox1 without creating another new
Form1? What have I done wrong?
Any help will be appreciated.

Jason
 
G

Guest

Jason Huang said:
My question is when I was in Form2, how can I return to the original Form1
and change the value of the Form1.textBox1 without creating another new
Form1? What have I done wrong?
Any help will be appreciated.

Two things. First, you need to create a form2 constructor that takes a
variable of type Form1 as a paramemter, and assign it to a Form1 variable
inside Form2. Then you can do this.theForm1.foo from within Form2.

Second, you need a way to access the text in form1. You could make textBox1
public, but doing that would be a violation of good OOP design. Instead you
should create a ChangeTextbox1Text(string s) method in Form1 and make it
public. Alternately you could do the same thing using a property.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top