单击角落中的Big Red X时未调用FormClosing

时间:2011-08-16 17:17:30

标签: c# winforms formclosing

我发现了这个:

Button with an X at the upper-right corner of the form, how to catch this event @ C#

其中说我应该使用FormClosing事件找出窗口因为点击X而关闭的时间。

但我的事件代码永远不会被调用:

private void MainWin_FormClosing(Object sender, FormClosingEventArgs e)
{
    m_closeThread = true;
    Application.Exit();
}

我必须遗漏一些基本的东西,但我不知道是什么。

2 个答案:

答案 0 :(得分:8)

您必须订阅以下内容:

this.FormClosing += this.MainWin_FormClosing;

在表单的构造函数(或某处)中,或使用:

override void OnFormClosing(FormClosingEventArgs e)
{
    m_closeThread = true;
    Application.Exit();
}

答案 1 :(得分:1)

确保您正确订阅了FormClosing活动。

你必须在你的MainWin对话框中(在构造函数中),如下所示:

this.FormClosing += new FormClosingEventHandler(MainWin_FormClosing);

希望它有所帮助。