Solved

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

Posted on 2007-11-20
9
247 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
 
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
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.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now