How to prevent or block closing a WinForms window?

后端 未结 8 614
情书的邮戳
情书的邮戳 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;
        }
    }
    
    0 讨论(0)
  • 2020-12-01 04:34

    Within your OnFormClosing event you can show the dialog and if answer is false (to not show) then set the Cancel property of the EventArgs to true.

    0 讨论(0)
  • 2020-12-01 04:34

    A special twist might be to always prevent just the user closing the form:

    private void Frm_FormClosing(object sender, FormClosingEventArgs e)
    {
        e.Cancel = (e.CloseReason == CloseReason.UserClosing); 
        // disable user closing the form, but no one else
    }
    
    0 讨论(0)
  • 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 
    };
    
    0 讨论(0)
  • 2020-12-01 04:46

    Straight from 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...
          }
       }
    }
    

    In your case you don't need to do anything to explicitly close the form. Unless you cancel it it will close automatically, so your code would be:

    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 讨论(0)
  • 2020-12-01 04:49
    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 is to enable or disable form closing event. For example, e.Cancel = true will disable you closing.

    0 讨论(0)
提交回复
热议问题