Closing a calling form (C#.Net)

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

Guest

Greetings,

I have the following code (which displays a second form), but it is not
working as expected. I want to close the calling form after the dismissing
the second form. However, the second form never displays with the code I
have below. Any suggestions? [Note: I have also placed the code,
"frmMain.ActiveForm.Close();" in the "Click" event of the command button on
the second form, but this is not working either].

ActiveForm.Hide();
frmInstructions frmInstructions = new frmInstructions();
frmInstructions.Show();
ActiveForm.Close();

Thanks!
 
Sherwood, chances are the form is "closing" before it displays. If you need
to suspend the main thread for the subform to open and then close, use the
following...

frmInstructions frmInstructions = new frmInstructions();
DialogResult dr = frmInstructions.ShowDialog();

this will also tell you how the form was closed, if you are interested...Chuck
 
Hi Chuck,

Thanks so much for the code. The dialog is closing. However, when the
routine finishes I am still in "run" mode. Is there something obvious I am
overlooking in the code below? Do I need to "unload" the main form in order
to exit "run" mode?

private void btnClickMe_Click(object sender, System.EventArgs e)
{
ActiveForm.Hide();
frmInstructions frmInstructions = new frmInstructions();
DialogResult dr = frmInstructions.ShowDialog();
}

Thanks again!

Sherwood

chuck rudolph said:
Sherwood, chances are the form is "closing" before it displays. If you need
to suspend the main thread for the subform to open and then close, use the
following...

frmInstructions frmInstructions = new frmInstructions();
DialogResult dr = frmInstructions.ShowDialog();

this will also tell you how the form was closed, if you are interested...Chuck

Sherwood said:
Greetings,

I have the following code (which displays a second form), but it is not
working as expected. I want to close the calling form after the dismissing
the second form. However, the second form never displays with the code I
have below. Any suggestions? [Note: I have also placed the code,
"frmMain.ActiveForm.Close();" in the "Click" event of the command button on
the second form, but this is not working either].

ActiveForm.Hide();
frmInstructions frmInstructions = new frmInstructions();
frmInstructions.Show();
ActiveForm.Close();

Thanks!
 
Sherwood, I am not sure what you want to do. If you want to close the form
that is displaying the insturctions then your code should look like...
private void btnClickMe_Click(object sender, System.EventArgs e)
{
frmInstructions frmI = new frmInstructions();
DialogResult dr = frmI.ShowDialog();
this.Close();
}

I am not sure what you have your "hide" in there. If you want the main form
to disappear while the Instructions are up then that will work.

Sherwood said:
Hi Chuck,

Thanks so much for the code. The dialog is closing. However, when the
routine finishes I am still in "run" mode. Is there something obvious I am
overlooking in the code below? Do I need to "unload" the main form in order
to exit "run" mode?

private void btnClickMe_Click(object sender, System.EventArgs e)
{
ActiveForm.Hide();
frmInstructions frmInstructions = new frmInstructions();
DialogResult dr = frmInstructions.ShowDialog();
}

Thanks again!

Sherwood

chuck rudolph said:
Sherwood, chances are the form is "closing" before it displays. If you need
to suspend the main thread for the subform to open and then close, use the
following...

frmInstructions frmInstructions = new frmInstructions();
DialogResult dr = frmInstructions.ShowDialog();

this will also tell you how the form was closed, if you are interested...Chuck

Sherwood said:
Greetings,

I have the following code (which displays a second form), but it is not
working as expected. I want to close the calling form after the dismissing
the second form. However, the second form never displays with the code I
have below. Any suggestions? [Note: I have also placed the code,
"frmMain.ActiveForm.Close();" in the "Click" event of the command button on
the second form, but this is not working either].

ActiveForm.Hide();
frmInstructions frmInstructions = new frmInstructions();
frmInstructions.Show();
ActiveForm.Close();

Thanks!
 
Back
Top