First, close the form
Exiting the WinForm program in C # includes a number of methods, such as: this. Close (); Application.exit (); Application.exitthread ();
System.Environment.Exit (0); When their respective methods are different, let's take a look at the details below.
1.this. Close (); Just close the current window, if it is not the main form, you can not quit the program, and if there is a managed thread (not the main threads), also can not cleanly exit;
2.application.exit (); Forces all messages to abort, exits all forms, but does not cleanly exit if there is a managed thread (not the main thread);
3.application.exitthread (); Force abort all messages on the calling thread, as well as the problem that other threads cannot exit correctly;
4.system.environment.exit (0); This is the most thorough exit method, no matter what threads are forced to quit, the program ends very clean.
Ii. closing of the login form and the main form
There are a lot of people in the C # to do the login form will encounter such a problem, login successful after the current login form closed, open the main form, but the main form point Close button after the program is still running in the process.
In fact, closing the main form simply closes the thread of the main form and does not close the program's main threads, that is, the main thread of the program is the login from.
Method One: (Frmlogin for login form, frmmain for main form)//login form in the Click event of the "Sign in" button
void button1_click (object sender, EventArgs e) { "123456"new this. Dispose (false
FormClosing Event or formclosed event code for the main form
private void Frmmain_formclosing (object sender, FormClosingEventArgs e)
{
Application.exit ();
}
FormClosed event code for the main form
private void Frmmain_formclosed (object sender, Formclosedeventargs e)
{
Application.exit ();
}
Description: FormClosing Event or formclosed event two select one, the difference between the two is that formclosed occurs after closing, the form's closing action cannot be canceled;
FormClosing occurs before closing, can be canceled, as long as the inside use e.cancel=true; You can make the window not close.
Advanced Technique: Pop up the FormClosing event code of the Confirmation exit dialog/main form before exiting the program
private void Frmmain_formclosing (object sender, FormClosingEventArgs e)
{
if (MessageBox.Show ("Do you really want to quit the program?") "," Exit program ", messageboxbuttons.okcancel) = = DialogResult.Cancel)
{
E.cancel = true;
}
}
FormClosed event code for the main form
private void Frmmain_formclosed (object sender, Formclosedeventargs e)
{
Dispose ();
Application.exit ();
}
Tip: The following code causes two dialog boxes to appear that determine exit, and the first time you click OK the second time you click any one will exit.
private void Frmmain_formclosing (object sender, FormClosingEventArgs e)
{
if (MessageBox.Show ("Do you really want to quit the program?") "," Exit program ", messageboxbuttons.okcancel) = = DialogResult.OK)
{
Application.exit ();
}
Else
{
E.cancel = true;
}
}
Analysis: The first time you click the "OK" button on the exit dialog, Application.exit () will cause the second frmmain_formclosing () event to be triggered, so a dialog box with two confirmation exits is displayed, but the selection of the second dialog box has no effect on whether or not to exit. You can instead determine if you clicked not the Confirm button E. Cancel = true; , make sure the button does not write anything or do not judge, if you want to add application.exit (); You can add it to the form's formclosed event. Method Two:
The Program.cs program asks the main () method code, where Frmlogin is the login form, frmmain the main form
static void Main ()
{
Application.enablevisualstyles ();
Application.setcompatibletextrenderingdefault (FALSE);
Frmlogin fl = new Frmlogin ();
if (fl. ShowDialog () = = DialogResult.OK)
{
Application.Run (New Frmmain ());
}
}
Sign In button code in the Sign-in form
private void Button1_Click (object sender, EventArgs e)
{
if (txtName.Text = = "123456")
{
This. DialogResult = DialogResult.OK;
This. Close ();
}
}
Transferred from: http://www.cnblogs.com/HappyEDay/p/5713707.html
Summary of WinForm Program exit techniques in C # (GO)