如何防止或阻止关闭WinForms窗口?

75

如何通过显示 MessageBox 来防止窗口关闭?(技术:使用 C#WinForms

当关闭事件发生时,我希望运行以下代码:

private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
    var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );

    if (closeMsg == DialogResult.Yes) {
        //close addFile form
    } else {
        //ignore closing event
    }
}

防止您的Winform关闭或消息框? - Bali C
7个回答

150
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
    var window = MessageBox.Show(
        "Close the window?", 
        "Are you sure?", 
        MessageBoxButtons.YesNo);

    e.Cancel = (window == DialogResult.No);
}

32

捕获FormClosing事件并设置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;
    }
}

很棒的答案。如果你真的想要的话,你可以将调用MessageBox.Show()内联,但这正是OP需要做的。 - KeithS
如果他想要询问用户“您有未保存的数据!在退出之前是否要保存?”他将有三种结果,是,否和取消。因此,为了将来进行进一步处理,最好准备好res。 - BlueM

8
一个特别的变化可能是始终防止用户关闭表单:
private void Frm_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = (e.CloseReason == CloseReason.UserClosing); 
    // disable user closing the form, but no one else
}

7

在你的 OnFormClosing 事件中,你可以展示对话框并且如果用户选择不展示,那么将 EventArgsCancel 属性设置为 true


3
为了在特定情况下防止或阻止表单关闭,您可以使用以下策略:
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{

    if (FLAG_CONDITION == true)
    {
        MessageBox.Show("To exit save the change!!");
        e.Cancel = true;
    }

}

2
直接引用自MSDN官网:

当表单即将关闭时发生。

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
   // Determine if text has changed in the textbox by comparing to original text. 
   if (textBox1.Text != strMyOriginalText)
   {
      // Display a MsgBox asking the user to save changes or abort. 
      if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
         MessageBoxButtons.YesNo) ==  DialogResult.Yes)
      {
         // Cancel the Closing event from closing the form.
         e.Cancel = true;
         // Call method to save file...
      }
   }
}

在您的情况下,您不需要做任何事来显式关闭表单。除非您取消它,否则它将自动关闭,因此您的代码应该是:

private void addFile_FormClosing( object sender, FormClosingEventArgs e ) {
    var closeMsg = MessageBox.Show( "Do you really want to close?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question );

    if (closeMsg == DialogResult.Yes) {
        // do nothing
    } else {
        e.Cancel = true;
    }
}

0

当关闭窗体时,您可以运行任何代码,然后隐藏窗体而不是关闭它,以防止其被释放。

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 
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接