Hide form instead of closing when close button clicked

C#.NetWinforms

C# Problem Overview


When a user clicks the X button on a form, how can I hide it instead of closing it?

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

C# Solutions


Solution 1 - C#

Like so:

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

(via Tim Huffman)

Solution 2 - C#

I've commented in a previous answer but thought I'd provide my own. Based on your question this code is similar to the top answer but adds the feature another mentions:

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

If the user is simply hitting the X in the window, the form hides; if anything else such as Task Manager, Application.Exit(), or Windows shutdown, the form is properly closed, since the return statement would be executed.

Solution 3 - C#

From MSDN:

> To cancel the closure of a form, set the Cancel property of the FormClosingEventArgs passed to your event handler to true.

So cancel then hide.

Solution 4 - C#

If you want to use the show/hide method I've actually done this myself for a menu structure a game I've recently done... This is how I did it:

Create yourself a button and for what you'd like to do, for example a 'Next' button and match the following code to your program. For a next button in this example the code would be:

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 snippet of the code 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
    }

So there is a show/hide method few lines of code, rather than doing a massive piece of code for such a simple task. I hope this helps to solve your problem.

Solution 5 - C#

Based on other response, 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();
        }
    }

According MSDN, the override is preferred:

> The OnFormClosing method also allows derived classes to handle the > event without attaching a delegate. This is the preferred technique > for handling the event in a derived class.

Solution 6 - C#

Note that when doing this (several answers have been posted) that you also need to find a way to ALLOW the user to close the form when they really want to. This really becomes a problem if the user tries to shut down the machine when the application is running, because (at least on some OS) this will stop the OS from shutting down properly or efficiently.

The way I solved this was to check the stack trace - there are differences between when the user tries to click the X vs when the system tries to end the application in preparation for shutdown.

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;
    }
}

Solution 7 - C#

This is the behavior of Modal forms. When you use form.ShowDialog() you are asking for this behavior. The reason for this is that form.ShowDialog doesn't return until the form is hidden or destroyed. So when the form is hidden, the pump inside form.ShowDialog destroys it so that it can return.

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

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

When you use modeless forms that are not children of a modal form, then you also need to run a message pump using Application.Run or Application.DoEvents in a loop. If the thread that creates a form exits, then the form will be destroyed. If that thread doesn't run a pump then the forms it owns will be unresponsive.

Edit: this sounds like the sort of thing that the ApplicationContext is designed to solve. http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx

Basically, you derive 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);

Your application context will need to know when it's ok to exit the application and when having the form(s) hidden should not exit the application. When it's time for the app to exit. Your application context or form can call the application context's ExitThread() method to terminate the message loop. At that point Application.Run() will return.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniTEggView Question on Stackoverflow
Solution 1 - C#AlexView Answer on Stackoverflow
Solution 2 - C#LizBView Answer on Stackoverflow
Solution 3 - C#Jorge CórdobaView Answer on Stackoverflow
Solution 4 - C#CraigView Answer on Stackoverflow
Solution 5 - C#OraceView Answer on Stackoverflow
Solution 6 - C#Michael BrayView Answer on Stackoverflow
Solution 7 - C#John KnoellerView Answer on Stackoverflow