How to prevent or block closing a WinForms window?

后端 未结 8 735
情书的邮戳
情书的邮戳 2020-12-01 04:01

How can I prevent window closing by showing a MessageBox? (Technology:WinForms with C#)

When the close event occurs I want the

8条回答
  •  囚心锁ツ
    2020-12-01 04:39

    You can run any code you want when closing the form then, hide the form instead of closing it to prevent it from being disposed

    yourFormName.FormClosing += (s, e) =>
    {
       // any code you want
       yourFormName.Hide(); // this hides the form
       e.Cancel = true;  // this cancels the close event, you can put any boolean exprission 
    };
    

提交回复
热议问题