WinForms Form won't close on pressing X or Close() in C#

后端 未结 2 809
挽巷
挽巷 2021-01-13 20:31

I\'m having a bit weird problem with WinForm which seems to refuse to close for some weird reason. I\'ve got very simple gui which sometimes doesn\'t react for me pressing X

相关标签:
2条回答
  • 2021-01-13 21:05

    Paste this code into your form classes:

        protected override void OnFormClosing(FormClosingEventArgs e) {
            e.Cancel = false;
            base.OnFormClosing(e);
        }
    

    When that works, you want to find out why you have Validating event handlers that don't want the form to be closed.

    Next thing you want to verify is Debug + Exceptions, tick the Thrown box for CLR Exceptions. This makes sure you don't swallow an exception that prevents a form from closing. Or worse, the operating system swallowing the exception, a nasty Windows 7 problem.

    0 讨论(0)
  • 2021-01-13 21:26

    If you are getting an Exception in your close method, then the Base closing method is never called.

    Put a try{}catch{} around everything

    0 讨论(0)
提交回复
热议问题