更改单击关闭窗口按钮的操作

时间:2011-03-13 18:59:52

标签: c# winforms

我希望标题栏上的“x”按钮关闭整个应用程序,而不是当前的表单。我该怎么做?

2 个答案:

答案 0 :(得分:4)

在表单上为FormClosed事件连接一个事件处理程序,然后调用Application.Exit()。例如:

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
    Application.Exit();
}

答案 1 :(得分:2)

@rsbarro是对的,但我希望更具体。

private void Form_FormClosed(object sender, FormClosingEventArgs e)
{
    // if close button clicked, not the computer is being shutdown or anyother reason
    if (e.CloseReason == CloseReason.UserClosing)
    {
        Application.Exit();
    }
}