Form Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

This should be easy but I am obviously not getting it. I need to be able to
click a button on form1 and it opens up form2. The user needs to be able to
do some input on form2, send it back to form1, and then form2 can close. How
do I do this?

I have been using from Form1:

Form2 newForm = new Form2();
newForm.ShowDialog();

This will bring up the new form and work but there is no link between the
two forms.

Any help would be appreciated, sorry for the ignorance.

-LMIT
 
What type of information are you looking to send back to form1? One way to
do this would be to expose the information publically through form2 so that,
if necessary, form1 may retrieve it. This would be more inline with how the
built-in dialogs, such as the OpenFileDialog and its Filename property,
work.
 
Something similar to the following code should work. Note that the behavior
is to allow the user to back out of the dialog by only updating information
if the dialog returns "OK". This would happen if you explicitly set the
DialogResult of Form2 to "OK" or, assuming Pocket PC, this will be done for
you if the user taps the "OK" button on the title bar.

public class Form1 : System.Windows.Forms.Form
{

...

private void button1_Click(object sender, System.EventArgs e)
{
Form2 f = new Form2();
if (f.ShowDialog() == DialogResult.OK)
{
// Do something with f.StringOne
// Do something with f.StringTwo
this.textBox1.Focus();
}
f.Dispose();
}

...

}

public class Form2 : System.Windows.Forms.Form
{

...

public string StringOne
{
get
{
return this.textBox1.Text;
}
}

public string StringTwo
{
get
{
return this.textBox2.Text;
}
}

...

}
 
Back
Top