【问题标题】:FormClosing not called when clicking Big Red X in the corner单击角落中的大红色 X 时未调用 FormClosing
【发布时间】:2011-08-16 17:17:29
【问题描述】:

我发现了这个:

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();
}

我一定错过了一些基本的东西,但我不知道是什么。

【问题讨论】:

  • 事件是否已委托给处理程序?

标签: c# winforms formclosing


【解决方案1】:

您必须订阅以下事件:

this.FormClosing += this.MainWin_FormClosing;

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

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

【讨论】:

  • 太棒了,做到了。我假设一个表单在创建时已经订阅了这个事件。
【解决方案2】:

确保您正确订阅了FormClosing 事件。

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

this.FormClosing += new FormClosingEventHandler(MainWin_FormClosing);

希望对你有帮助。

【讨论】:

  • 或者更短的版本this.FormClosing += MainWin_FormClosing; :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多