How to prevent or block closing a WinForms window?

C#Winforms

C# Problem Overview


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

When the close event occurs I want the following code to be run:

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

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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

Solution 3 - C#

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
}

Solution 4 - C#

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.

Solution 5 - C#

For prevent or block the form closing in particular situation you can use this strategy:

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{

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

}

Solution 6 - C#

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

Solution 7 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1678541View Question on Stackoverflow
Solution 1 - C#VictorView Answer on Stackoverflow
Solution 2 - C#BlueMView Answer on Stackoverflow
Solution 3 - C#Lars I NielsenView Answer on Stackoverflow
Solution 4 - C#Erre EfeView Answer on Stackoverflow
Solution 5 - C#daniele3004View Answer on Stackoverflow
Solution 6 - C#D StanleyView Answer on Stackoverflow
Solution 7 - C#Mostafa WaelView Answer on Stackoverflow