我正在使用带有确定和取消按钮的表单。当用户单击“取消”按钮时,用户将收到一条消息以确认表单是否应该关闭。单击确定 = 关闭,但是当单击取消时,表单不应该关闭,但这就是正在发生的事情,我已经测试为表单添加一些事件代码,但仍然关闭。我该怎么做才能让它正常工作?
// Button - Cancel
private void btnCancel_Click(object sender, EventArgs e)
{
// Message box to confirm or not
if (MessageBox.Show("Do you really want to cancel and discard all data?",
"Think twice!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) ==
DialogResult.Yes)
{
// Yes
//this.Close(); // Closes the contact form
m_closeForm = false;
}
else
{
m_closeForm = false;
// No
// Do nothing, the user can still use the form
}
}
private void ContactForm_Formclosing(object sender, FormClosingEventArgs e)
{
if (m_closeForm)
e.Cancel = false; // Stänger formuläret. Inget skall hända
else
e.Cancel = true; // Stänger inte formuläret
}