我有一个消息框的代码:
private void ControlComp_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
if (changed == true)
{
string message = "Do you want to exit without saving?";
string caption = "exit";
MessageBoxButtons buttons = MessageBoxButtons.YesNo;
MessageBoxIcon icon = MessageBoxIcon.Question;
DialogResult result;
result = MessageBox.Show(message, caption, buttons, icon);
if (result == DialogResult.Yes)
{
this.Close();
}
}
}
我需要做的是,我检查更改的值是否为真,只有它是真的它会显示一个弹出消息,带有是或否按钮,如果单击是,我退出整个表单,否则我留在表单中
如果更改的值为false,则再次关闭它。 但是这段代码不起作用。
答案 0 :(得分:0)
要取消关闭表单,请在else语句中使用e.Cancel = true
;
见下面的示例代码: -
if (MessageBox.Show("This will close down the whole application. Confirm?", "Close Application", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
MessageBox.Show("The application has been closed successfully.", "Application Closed!", MessageBoxButtons.OK);
}
else
{
e.Cancel = true;
}