Solved

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

Posted on 2007-11-20
9
260 Views
Last Modified: 2013-12-17
Hi,

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

How may I do so?

Thanks.
0
Comment
Question by:EEssam
  • 5
  • 4
9 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20325140
inside your form:

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

Author Comment

by:EEssam
ID: 20325156
Worked but I got:

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

When Application.Exit() was run.
0
 

Author Comment

by:EEssam
ID: 20325174
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20325179
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
 

Author Comment

by:EEssam
ID: 20325216
The same error. Everything is cool when I remove:

else
                  base.OnFormClosing(e);

Why should I keep it?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20325247
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
 

Author Comment

by:EEssam
ID: 20325274
OK. What does base.OnFormClosing(e); exactly do?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20325293
0
 

Author Closing Comment

by:EEssam
ID: 31410283
Since I just want to hide the form, I just removed: else
                  base.OnFormClosing(e);
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question