Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent or block closing a WinForms window?

Tags:

c#

winforms

People also ask

How do I stop a windows form from closing?

To cancel the closure of a form, set the Cancel property of the CancelEventArgs passed to your event handler to true .

Which of the following method is used to close the windows form application?

The Exit method stops all running message loops on all threads and closes all windows of the application.

Does Microsoft still support WinForms?

"We continue to support and innovate in Windows Forms runtime," said Microsoft's Igor Velikorossov last month in announcing what's new for WinForms in . NET 6. He's a software engineer on the dev team for the 19-year-old product, a free and open-source graphical (GUI) class library included as a part of .

How do I turn off WinForms full screen?

The only thing you can do is completely disable resizing your window. In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle. FixedSingle , FormBorderStyle. Fixed3D , or FormBorderStyle.


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

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

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.


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
}

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

}

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