Solved

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

Posted on 2007-11-20
9
267 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

830 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