【问题标题】:C# form doesn't close on FormClosing eventC# 表单不会在 FormClosing 事件中关闭
【发布时间】:2010-10-04 02:09:25
【问题描述】:

在我将以下代码添加到我的代码隐藏后,我的表单没有关闭。

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    MyThreadingObj.Dispose();
}

【问题讨论】:

  • 你正在处理的 obj 是什么类型的对象?
  • 将 dispose 包装在 try/catch 中,并显示或记录您遇到的任何异常 - 这可能会给您一个提示。并呼应@Web 所说的话:你为什么要在那时处理obj 的任何内容?

标签: c# .net winforms formclosing


【解决方案1】:

听起来添加上述代码会阻止您的Form 关闭。如果是这样,那么最可能的原因是 MyTHreadingObj.Dispose() 语句正在引发异常。尝试将语句包装在 try/catch 中,看看是否是这种情况

try {
  MyThreadingObj.Dispose();
} catch ( Exception e ) { 
  Console.WriteLine(e);
}

【讨论】:

    【解决方案2】:
    protected override void OnFormClosing(FormClosingEventArgs e)
            {            
                base.OnFormClosing(e);
                if (PreClosingConfirmation() == System.Windows.Forms.DialogResult.Yes)
                {
                    Dispose(true);
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
    
            private DialogResult PreClosingConfirmation()
            {
                DialogResult res = System.Windows.Forms.MessageBox.Show(" Do you want to quit?          ", "Quit...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                return res;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2014-06-17
      • 1970-01-01
      • 2023-03-31
      • 2012-07-13
      • 2017-06-15
      • 1970-01-01
      相关资源
      最近更新 更多