There are two main ways to show a form:
1) non-modal display of form: here the child form and parent form are both active and can accept input.
private void ShowForm2_Click(object sender, EventArgs e)
{
Form2 myForm2 = new Form2();
myForm2.Show();
}
2) modal display of a form: here once the parent activates the child form with myForm2.ShowDialog(), only the child can accept user input. The parent is frozen until the child form closes. Code is frozen on the parent form at the ShowDialog call til the child form closes. The child form can then pass a dialog result to the parent form (which button was pushed on the child form) via the result of the ShowDialog method. Note that the child form is not destroyed and must be disposed of by the coder. I use a using block to automatically close the child form when I'm done with it:
private void ShowForm2_Click(object sender, EventArgs e)
{
// create a child form in a using block so it will be disposed of when done
using (Form2 myForm142 new Form2())
{
if (myForm2.ShowDialog() == DialogResult.OK)
// my parent form code is stuck here til the child form complete's its
// action and returns a dialogResult.
{
// do stuff here if the user pressed the "OK" button on the child form
}
}
}
Enter your message below
Sign in or Join us (it's free).