Close C # whether to close the main form dialog box appears

Source: Internet
Author: User

When developing a system, there is often a problem: when you close the main form, that is, exit the system, if you want to prompt whether to close it to avoid misoperation, you can add a dialog box in the Main_FormClosing event of the main form. The Code is as follows:

Private void Main_FormClosing (object sender, FormClosingEventArgs e)

{
DialogResult TS = MessageBox. Show ("exit? "," Prompt ", MessageBoxButtons. YesNo, MessageBoxIcon. Question );
If (TS = DialogResult. Yes)
E. Cancel = false;
Else
E. Cancel = true;

}

In this way, click "yes" to exit the system, and "no" to close the dialog box without exiting the system. It is worth noting that this idea can also be used to indicate whether to save the operations performed on a form when closing a form. It is also in the FormClosing event of the form, as shown below:

Private void Education_Train_Mark_KeyIn_FormClosing (object sender, FormClosingEventArgs e)
{
If (! Saved)
{
DialogResult TS = MessageBox. Show ("Save? "," Prompt ", MessageBoxButtons. YesNo, MessageBoxIcon. Question );
If (TS = DialogResult. Yes)
{
Button#click (sender, e); // click the event of the Save OPERATION button
E. Cancel = false;
}
Else
E. Cancel = false;
}
}

The preceding saved defines a bool variable to determine whether a save operation is performed. If it is not saved in advance, click "yes" to save it, and then close the form. If you click "no", close the form directly.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.