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.