How to prevent or block closing a WinForms window?

后端 未结 8 702
情书的邮戳
情书的邮戳 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:31

    Catch FormClosing event and set e.Cancel = true

    private void AdminFrame_FormClosing(object sender, FormClosingEventArgs e)
    {
        var res = MessageBox.Show(this, "You really want to quit?", "Exit",
                MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
        if (res != DialogResult.Yes)
        {
          e.Cancel = true;
          return;
        }
    }
    

提交回复
热议问题