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

后端 未结 2 810
挽巷
挽巷 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.

提交回复
热议问题