How to Disable Alt + F4 closing form?

C#.NetWinforms

C# Problem Overview


What is the best way to disable Alt + F4 in a c# win form to prevent the user from closing the form?

I am using a form as a popup dialog to display a progress bar and I do not want the user to be able to close it.

C# Solutions


Solution 1 - C#

This does the job:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = true;
}

Edit: In response to pix0rs concern - yes you are correct that you will not be able to programatically close the app. However, you can simply remove the event handler for the form_closing event before closing the form:

this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Close();

Solution 2 - C#

If you look at the value of FormClosingEventArgs e.CloseReason, it will tell you why the form is being closed. You can then decide what to do, the possible values are:

Member name - Description


None - The cause of the closure was not defined or could not be determined.

WindowsShutDown - The operating system is closing all applications before shutting down.

MdiFormClosing - The parent form of this multiple document interface (MDI) form is closing.

UserClosing - The user is closing the form through the user interface (UI), for example by clicking the Close button on the form window, selecting Close from the window's control menu, or pressing ALT+F4.

TaskManagerClosing - The Microsoft Windows Task Manager is closing the application.

FormOwnerClosing - The owner form is closing.

ApplicationExitCall - The Exit method of the Application class was invoked.

Solution 3 - C#

I believe this is the right way to do it:

protected override void OnFormClosing(FormClosingEventArgs e)
{
  switch (e.CloseReason)
  {
    case CloseReason.UserClosing:
      e.Cancel = true;
      break;
  }

  base.OnFormClosing(e);
}

Solution 4 - C#

Note that it is considered bad form for an application to completely prevent itself from closing. You should check the event arguments for the Closing event to determine how and why your application was asked to close. If it is because of a Windows shutdown, you should not prevent the close from happening.

Solution 5 - C#

You could handle the FormClosing event and set FormClosingEventArgs.Cancel to true.

Solution 6 - C#

> I am using a form as a popup dialog to display a progress bar and I do not want the user to be able to close it.

If the user is determined to close your app (and knowledgeable) enough to press alt+f4, they'll most likely also be knowledgeable enough to run task manager and kill your application instead.

At least with alt+f4 your app can do a graceful shutdown, rather than just making people kill it. From experience, people killing your app means corrupt config files, broken databases, half-finished tasks that you can't resume, and many other painful things.

At least prompt them with 'are you sure' rather than flat out preventing it.

Solution 7 - C#

This is a hack to disable Alt + F4.

private void test_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this.ModifierKeys == Keys.Alt || this.ModifierKeys == Keys.F4) 
    { 
        e.Cancel = true; 
    }    
}

Solution 8 - C#

Subscribe FormClosing event

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = e.CloseReason == CloseReason.UserClosing;
}

Only one line in the method body.

Solution 9 - C#

This does the job:

bool myButtonWasClicked = false;
private void Exit_Click(object sender, EventArgs e)
{
  myButtonWasClicked = true;
  Application.Exit();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  if (myButtonWasClicked)
  {
    e.Cancel = false;
  }
  else
  {
    e.Cancel = true;
  }


}

Solution 10 - C#

Would FormClosing be called even when you're programatically closing the window? If so, you'd probably want to add some code to allow the form to be closed when you're finished with it (instead of always canceling the operation)

Solution 11 - C#

Hide close button on form by using the following in constructor of the form:

this.ControlBox = false;

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
QuestionRyan SampsonView Question on Stackoverflow
Solution 1 - C#MartinView Answer on Stackoverflow
Solution 2 - C#Matt WarrenView Answer on Stackoverflow
Solution 3 - C#antsyawnView Answer on Stackoverflow
Solution 4 - C#Lasse V. KarlsenView Answer on Stackoverflow
Solution 5 - C#TimboView Answer on Stackoverflow
Solution 6 - C#Orion EdwardsView Answer on Stackoverflow
Solution 7 - C#Brahim BourassView Answer on Stackoverflow
Solution 8 - C#linquizeView Answer on Stackoverflow
Solution 9 - C#CondorkunkaView Answer on Stackoverflow
Solution 10 - C#pix0rView Answer on Stackoverflow
Solution 11 - C#Bharath theorareView Answer on Stackoverflow