views:

1052

answers:

5

how do i hide() a form?

i would like when user clicks the X on the form to hide it instead of closing it.

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

please help.

+3  A: 

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

From msdn.

So Cancel then hide.

Jorge Córdoba
+7  A: 

The solution is to create a handler for the FormClosing event of the form, then force the form to be hidden, then, the important part, cancel the close event...

Eg:

// Use this event handler for the FormClosing event.
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
   this.Hide();
   e.Cancel = true; // this cancels the close event.
}
Alex
thanks very much to you both.
iEisenhower
how do i re enable to e.cancel = false? so that i can close the form later?
iEisenhower
i think i will just have add a flag that lets me now if i want to close it for real or not.
iEisenhower
+1  A: 

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. If I can find the code, I'll post what I did here in an update.

Update: found the code.. here it is:

    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;
        }
    }
Michael Bray
completely agree with what you've said.
Alex
im talking about your previous comment not the last one. yes although the above code works on my child form. but because of the e.Cancel i can no longer close down the parent form! any clues?
iEisenhower
for the above code to work i need to add namespace for StackTrace. please help by stating the namespace.
iEisenhower
found: using System.Diagnostics;Error 2 'System.Diagnostics.Log' is a 'type' but is used like a 'variable' C:\Users\iAmjad\Documents\Visual Studio 2008\Projects\BattleShips\Nietzsche.Battleships\NietzscheBattleships\NetworkForm.cs 56 21 NietzscheBattleships
iEisenhower
this seems unnecessarily complicated - why fight with modal form behavior in this way when you can just use modeless forms instead?
John Knoeller
im using modeless form. and the application is not closing because of e.cancel = true;
iEisenhower
@ikurtz: for the error with Log - take out my Log(...) statements... that references other code that I posted and I only left it in so that you can see the situation that that code block is handling.
Michael Bray
i took out the log. but on the parent form Application.Exit(); still does not close the application.
iEisenhower
i tried MessageBox.Show(methodName); and miExit_Click or PeekMessage does not show up in the list.
iEisenhower
@John I agree, Assuming .NET 2.0 or greater, Why not just check for e.CloseReason == CloseReason.WindowsShutDown ....http://msdn.microsoft.com/en-us/library/system.windows.forms.closereason%28VS.80,loband%29.aspx
ShaneB
ikurtz: sorry man, you have to read between the lines... that code is from real production code and as such is somewhat dependent on it. the mi_Exit is a menu item that I use to allow the user to ACTUALLY exit the form when they want to. PeekMessage of course only shows up when you are engaging in a system shutdown. However, reading Shane's comment, I think it is obvious that the method I presented (which was legacy code from .NET 1.1) isn't needed if you are using .NET 2.0, so just use his suggestion (I'm going to update my code, too!!)
Michael Bray
A: 

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.

John Knoeller
could you extend on Application.doevents please?i think i need it. adding e.Cancel = false; in parent form closing didnt work either.
iEisenhower
Is this your only form? your only thread?
John Knoeller
sorry i wasnt clear.the application shuts down when i click the X.but Application.Exit(); doesn't work.
iEisenhower
i have two forms. parent and child though this is not mdi.
iEisenhower
If this is the child form, and the parent form is modal, then you are fine. If this is the parent form, then you should probably use `Application.Run(MyAppContext)`
John Knoeller
im doing in program: Application.Run(new gameForm());but that i think is not the issue. for some reason when i do e.cancel = true; on the child form. the parent form takes this value and the application.exit() does not close the parent form.
iEisenhower
also this works: System.Environment.Exit(-1); works and quits but Application.Exit(); still does not work.
iEisenhower
ok. the application.Exit() is not working because the app cannot close the child form (e.Cancel = true)!#how do i re enable the e.Cancel = false?
iEisenhower
forget about the whole e.Cancel = false trick. It's bad advice, you should throw that code out. Just use form.Show instead of form.ShowDialog and your form will stop getting destroyed when you hide it.
John Knoeller
thanks have it sorted out now!
iEisenhower
+1  A: 

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) return;
        e.Cancel = true;
        Hide();
    }

If the user is simply hitting the X in the window the form hides, if any thing else such as task manager, application.exit, or windows shutdown the form is properly closed, since the return statement would be executed.

ShaneB