68

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

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

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

8 回答 8

139
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);
}
于 2012-10-13T13:49:02.367 回答
29

捕获 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;
    }
}
于 2012-09-13T15:18:15.247 回答
8

一个特殊的转折可能是始终阻止用户关闭表单:

private void Frm_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = (e.CloseReason == CloseReason.UserClosing); 
    // disable user closing the form, but no one else
}
于 2017-03-15T08:27:12.780 回答
7

在您的OnFormClosing事件中,您可以显示对话框并且if答案为 false(不显示),然后将Cancel属性设置EventArgstrue

于 2012-09-13T15:17:17.490 回答
2

为了防止或阻止在特定情况下关闭表单,您可以使用以下策略:

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{

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

}
于 2016-12-01T10:35:33.847 回答
1

直接来自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;
    }
}
于 2012-09-13T15:20:07.100 回答
0
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) {
    e.Cancel = false;
} else {
    e.Cancel = true;
}

e.Cancel 是启用或禁用表单关闭事件。例如,e.Cancel = true将禁用您关闭。

于 2019-03-24T04:38:02.590 回答
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 
};
于 2020-11-28T16:23:29.187 回答