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.
EEssamAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Jaime OlivaresSoftware ArchitectCommented:
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 OlivaresSoftware ArchitectCommented:
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 OlivaresSoftware ArchitectCommented:
0
 
EEssamAuthor Commented:
Since I just want to hide the form, I just removed: else
                  base.OnFormClosing(e);
0
All Courses

From novice to tech pro — start learning today.