• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • 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 OlivaresSoftware 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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