Hide the form, not close when you click the Close button

When the user clicks the X button on the form, how can I hide it and not close it?

I tried this.hide() in FormClosing , but it still closes the form.

+51
c # winforms
Jan 07 '10 at 16:05
source share
7 answers

Same:

 private void MyForm_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; Hide(); } } 

(via Tim Huffman )

+78
Jan 07
source share

I commented on the previous answer, but thought I would provide my own. Based on your question, this code is similar to the top answer, but adds a function that others mention:

 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; Hide(); } } 

If the user simply clicks X in the window, the form is hidden; if anything else, such as the task manager, Application.Exit() or shutting down Windows, the form closes correctly, as the return is executed.

+50
Jan 07 '10 at 18:00
source share

From MSDN :

To cancel closing a form, set the Cancel property to the FormClosingEventArgs value FormClosingEventArgs to your event handler to true .

So cancel, then hide.

+8
Jan 07
source share

If you want to use the show / hide method, I actually did it myself for the menu structure I recently did ... Here's how I did it:

Create a button for yourself and for what you want to do, for example, the "Next" button and compare the following code with your program. For the next button in this example, the code will look like this:

 btnNext.Enabled = true; //This enabled the button obviously this.Hide(); //Here is where the hiding of the form will happen when the button is clicked Form newForm = new newForm(); //This creates a new instance object for the new form CurrentForm.Hide(); //This hides the current form where you placed the button. 

Here is a piece of code that I used in my game to help you understand what I'm trying to explain:

  private void btnInst_Click(object sender, EventArgs e) { btnInst.Enabled = true; //Enables the button to work this.Hide(); // Hides the current form Form Instructions = new Instructions(); //Instantiates a new instance form object Instructions.Show(); //Shows the instance form created above } 

Thus, the show / hide method contains several lines of code, and does not do the bulk of the code for such a simple task. Hope this helps solve your problem.

+4
Nov 29 '12 at 6:52
source share

Please note that when doing this (multiple answers were sent) you also need to find a way to ALLOW the user to close the form when they really want it. This really becomes a problem if the user tries to turn off the machine when the application is running, because (at least on some OSs) this will stop the OS from working properly or efficiently.

As I decided, this is to check the stack trace - there are differences between when the user tries to press X vs, when the system tries to complete the application in preparation for shutting down.

 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { StackTrace trace = new StackTrace(); StackFrame frame; bool bFoundExitCommand = false; for (int i = 0; i < trace.FrameCount; i++) { frame = trace.GetFrame(i); string methodName = frame.GetMethod().Name; if (methodName == "miExit_Click") { bFoundExitCommand = true; Log("FormClosing: Found Exit Command ({0}) - will allow exit", LogUtilityLevel.Debug3, methodName); } if (methodName == "PeekMessage") { bFoundExitCommand = true; Log("FormClosing: Found System Shutdown ({0}) - will allow exit", LogUtilityLevel.Debug3, methodName); } Log("FormClosing: frame.GetMethod().Name = {0}", LogUtilityLevel.Debug4, methodName); } if (!bFoundExitCommand) { e.Cancel = true; this.Visible = false; } else { this.Visible = false; } } 
+3
Jan 07 '10 at 16:10
source share

This is the behavior of modal forms. When you use form.ShowDialog() , you ask for it. The reason for this is that form.ShowDialog does not return until the form is hidden or destroyed. Therefore, when the form is hidden, the pump inside the .ShowDialog form destroys it so that it can return.

If you want to show and hide the form, then you should use the Modelless dialog box model http://msdn.microsoft.com/en-us/library/39wcs2dh(VS.80).aspx

form.Show() returns immediately, you can show and hide this window that you want, and it will not be destroyed until you explicitly destroy it.

When you use non-modal forms that are not child modules, you also need to start the message pump using Application.Run or Application.DoEvents in a loop. If the stream that creates the form terminates, the form will be destroyed. If this thread does not start the pump, then the forms that it owns will be immune.

Edit: this looks like ApplicationContext is for the solution. http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx

Basically, you get a class from ApplicationContext, pass an instance of your ApplicationContext as an argument to Application.Run()

 // Create the MyApplicationContext, that derives from ApplicationContext, // that manages when the application should exit. MyApplicationContext context = new MyApplicationContext(); // Run the application with the specific context. Application.Run(context); 

The context of your application should know when it is suitable for exiting the application, and when hidden forms should not exit the application. When it's time to exit the application. The application context or form can call the ExitThread() application context method to end the message loop. At this point, Application.Run() will return.

Without knowing more about the heriarchy of your forms and your rules for deciding when to hide forms and when to exit, it is impossible to be more specific.

+2
Jan 07 '10 at 16:27
source share

Based on another answer, you can put it in your form code:

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

Overriding is preferred: MSDN "The OnFormClosing method also allows derived classes to handle the event without attaching a delegate. The preferred method of handling the event is in the derived class."

+1
Sep 08 '14 at 15:25
source share



All Articles