C# Show/Close a form

C++, C#, Java, PHP, ect...
Post Reply
crzyone9584
Posts: 116
Joined: Mon Aug 01, 2011 4:46 am

C# Show/Close a form

Post 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?
Imagecrzy
Male Ninja
Str: 5 Agi: 17 Fort: 5 IQ: 5
HP: 10/10 MP: 5/5
See Full Character:
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: C# Show/Close a form

Post 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.
The indelible lord of tl;dr
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: C# Show/Close a form

Post 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.
crzyone9584
Posts: 116
Joined: Mon Aug 01, 2011 4:46 am

Re: C# Show/Close a form

Post 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.
Imagecrzy
Male Ninja
Str: 5 Agi: 17 Fort: 5 IQ: 5
HP: 10/10 MP: 5/5
See Full Character:
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: C# Show/Close a form

Post 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.
The indelible lord of tl;dr
Post Reply

Return to “Coding”