Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 表格关闭事件_C#_Winforms - Fatal编程技术网

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,无需询问。