表格闭幕活动

时间:2013-11-30 16:05:33

标签: 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.Warning);

    if (result == DialogResult.Yes)
    {
        Application.Exit();
    }
    else if(result == DialogResult.No)
    {
        //How to keep the form here!!!
    }
}

1 个答案:

答案 0 :(得分:2)

将您的代码放入Form Closing事件中,并在else if语句中添加e.Cancel = true NO

您将使用表单结束事件,因为当表单已关闭且您无法阻止已关闭的表单关闭时,将抛出Form Closed。

else if (result == DialogResult.No)
{
    e.Cancel = true;
}