Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

How can I call this.Hide(); when the app is closed by clicking the X

Hi,

I want to only hide the form when the title bar red X is clicked.

How may I do so?

Thanks.
0
EEssam
Asked:
EEssam
  • 5
  • 4
1 Solution
 
Jaime OlivaresCommented:
inside your form:

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)  
            {
                       e.Cancel = true;
                       this.Hide();
             }
              else
                  base.OnFormClosing(e);
        }
0
 
EEssamAuthor Commented:
Worked but I got:

An unhandled exception of type 'System.StackOverflowException' occurred in app.exe

When Application.Exit() was run.
0
 
EEssamAuthor Commented:
Please note:

1. When I comment the two lines:

else
                base.OnFormClosing(e);

The error goes...

2. The form I applied your code to is not the main form and I'm closing the application from the main form.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jaime OlivaresCommented:
Hmm, not sure at this moment. Try to remove the 'else'


protected override void OnFormClosing(FormClosingEventArgs e)
{
     if (e.CloseReason == CloseReason.UserClosing)  
     {
         e.Cancel = true;
         this.Hide();
     }
     base.OnFormClosing(e);
}

Open in new window

0
 
EEssamAuthor Commented:
The same error. Everything is cool when I remove:

else
                  base.OnFormClosing(e);

Why should I keep it?
0
 
Jaime OlivaresCommented:
Because it is recommended by the MSDN documentation. Indeed I have an application with this approach, don't know why it is not working for you. Maybe it is interfering with other event.
0
 
EEssamAuthor Commented:
OK. What does base.OnFormClosing(e); exactly do?
0
 
Jaime OlivaresCommented:
0
 
EEssamAuthor Commented:
Since I just want to hide the form, I just removed: else
                  base.OnFormClosing(e);
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now