C# 表格关闭事件
我想保留表格。在以下代码中按下“否”按钮时C# 表格关闭事件,c#,winforms,C#,Winforms,我想保留表格。在以下代码中按下“否”按钮时 private void parent_window_FormClosed(object sender, FormClosedEventArgs e) { DialogResult result = MessageBox.Show("Do you really want to Logout!", "Log Conformation.", MessageBoxButtons.YesNo, MessageBoxIcon.Warni
private void parent_window_FormClosed(object sender, FormClosedEventArgs e)
{
DialogResult result = MessageBox.Show("Do you really want to Logout!",
"Log Conformation.", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
Application.Exit();
}
else if(result == DialogResult.No)
{
//How to keep the form here!!!
}
}
将代码放入表单关闭事件中,并在else if语句中添加
e.Cancel=true
,表示否
您必须使用表单关闭事件,因为表单已关闭时会引发表单关闭,并且您无法阻止已关闭的表单关闭
else if (result == DialogResult.No)
{
e.Cancel = true;
}
FormClosed无法取消,它告诉您窗口已关闭,因此为时已晚。您必须使用FormClosing,不要忘记注意e.CloseReason。用户已经确定他想退出btw,无需询问。