Page 1 of 1

C# Show/Close a form

Posted: Sat Aug 27, 2011 7:41 pm
by crzyone9584
Right now i'm hiding my Login form. I really just want to close that form instead of hiding it and open up my main menu for my editors. But when i do

frmLogin.Close()
frmMainMenu.Show()

or have the .show on top, the application just closes. How would I get the main menu to stay open but close the login one.

Also I added

Code: Select all

private void frmMain_Closing(object sender, FormClosedEventArgs e)
        {
            // SendData.OutEditor;
            // Constants.isInEditor = false;

            Program.s_editor.Shutdown(" Good Bye "); 
        }
to my frmMain but it doesn't get called when i click the red x. Am i missing something?

Re: C# Show/Close a form

Posted: Sat Aug 27, 2011 8:33 pm
by Jackolantern
You are looking for the FormClosing event:

Code: Select all

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
            MessageBox.Show("We are closing out!");
        }
Or if you would rather go with a FormClosed, which occurs right after FormClosing:

Code: Select all

 private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
            MessageBox.Show("We are closed!");
        }
EDIT: Ohh, and use

Code: Select all

this.close();
Although it should be the same as what you are typing, it is supposed to only close the form that the code is used in, and should only close the application if it is the last one open.

However, it has been forever since I have made any Windows Forms, so I could be a bit rusty.

Re: C# Show/Close a form

Posted: Sun Aug 28, 2011 3:22 pm
by Xaleph
Jack is right, the formClosing can be used. Also, like Jack mentioned, this.close(); is a better option, or the classname.close(); Which is the same as this haha. Anyway, I also notice you use the _editor.close(); I`m not sure if that`s a declared object, but it might also cause a logic error.

Re: C# Show/Close a form

Posted: Sun Aug 28, 2011 6:42 pm
by crzyone9584
the editor.Shutdown is for the netowrking part. I've been trying to get it called when my frmMain is being closed but its not working.

Re: C# Show/Close a form

Posted: Sun Aug 28, 2011 10:06 pm
by Jackolantern
Have you debugged it to see if the editor.shutdown() gets put on the stack when the form closes? Maybe it is an issue in that method instead of with the event handler. Events are pretty simple, and typically work every time provided you have Visual Studio write it for you.