Closing a Form

  • Thread starter Thread starter Sherwood Page
  • Start date Start date
S

Sherwood Page

Greetings,

I have a "newbie" question related to forms. I have the following code
in which I display a second form when a button on the first form is
pressed (using the "Click" event).

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

What I have noticed, however, is that I am still in "run" mode when the
code finishes executing. I thought the reason might be due to the fact
that I never closed the first form, I just hid it.

However, when I add the line of code, "ActiveForm.Close();" after
"frmInstructions.Show();", I have noticed that the second dialog never
even displays. Is there something obvious I am doing wrong?

[Note: I have also placed the following code in the "Click" event of the
command button on the second form, but I receive an error on that line
of code: "frmMain.ActiveForm.Close();"].

Thanks in advance!
 
Sherwood,
If you look at the code for your main form, there should be a "static
Main( )" method. This method should contain code that is roughtly:
Application.Run( new Form1( ) );

This Application object's Run method basically makes the instance of the
Form1 form the main form of the application, when this instance dies,
exentially you application ends. Now, if you hide this form and open
another, then close the latter, the instance of your main form is still
alive, it's just hidden, you need to close it in order for your application
to terminate. The other thing that you tried was closing the Form1 instance
before starting the other one ... not going to work cause then you're
actually ending your application right there and then.


Cordell Lawrence
Teleios Systems Ltd.
 
Hi Cordell,

Thanks for the reply. However, I have tried placing
"ActiveForm.Close();" after the code that displays the second form, but
when I do this the second form (i.e., "frmInstructions") never even
displays (and I am still left in "run" mode). The code I currently have
is below:

Code for "frmMain" (the 1st form):

[STAThread]
static void Main()
{
Application.Run(new frmMain());
}

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

Code for "frmInstructions" (the 2nd form):

private void btnClose_Click(object sender, System.EventArgs e)
{
ActiveForm.Close();
}

Thanks again!

Sherwood
 
Sheerwood, Anytime you close the main for the application will die and your
second form will also die alone with it, thats why it doesn't show ... I'm
not sure what exactly your attempting to do, but this can be dealt with in
several was.
One of the simplest ways is to have the second form close the first (main)
form when it's done.
 
Hi,

I have also tried that (i.e., attempting to close the first form from
within the second form), but I receive the following error:

"An unhandled exception of type 'System.NullReferenceException' occurred
in Test.exe. Additional information: Object reference not set to an
instance of an object."

Here is the code in the 2nd form:

private void btnClose_Click(object sender, System.EventArgs e)
{
ActiveForm.Close();
frmMain.ActiveForm.Close(); // Is this syntax correct?
}

Is my syntax wrong for closing the first form (i.e., "frmMain")? (By
the way, the first form displays the second form when a button is
pressed. Once that 2nd form is displayed, the user then clicks the
"Close" button to dismiss the second form.)

Thanks in advance!

Sherwood
 
Before creating the second for set it parent to the first.

So

private void btnClickMe_Click(object sender, System.EventArgs e)
{
this.Hide();
frmInstructions frmInstructions = new FrmInstructions();
frmInstructions.Parent = this; // Set the parent of the child for to the
main form
frmInstructions.Show();
}

Close code FrmInstructions

private void btnClickMe_Click(object sender, System.EventArgs e)
{
if(this.Parent != null)
this.Parent.Close(); // in this case this will close the
entire app since the parent is the main form
}

Hope this helps
Cordell Lawrence
 
Back
Top