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

<p>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.</p>

[{'answer_id': 14949, 'author': 'Timbo', 'author_id': 1810, 'author_profile': 'https://Stackoverflow.com/users/1810', 'pm_score': 4, 'selected': False, 'text': '<p>You could handle the <code>FormClosing</code> event and set <code>FormClosingEventArgs.Cancel</code> to <code>true</code>.</p>\n'}, {'answer_id': 14960, 'author': 'Martin', 'author_id': 770, 'author_profile': 'https://Stackoverflow.com/users/770', 'pm_score': 8, 'selected': True, 'text': '<p>This does the job:</p>\n\n<pre><code>private void Form1_FormClosing(object sender, FormClosingEventArgs e)\n{\n    e.Cancel = true;\n}\n</code></pre>\n\n<p>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:</p>\n\n<pre><code>this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);\nthis.Close();\n</code></pre>\n'}, {'answer_id': 14984, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 2, 'selected': False, 'text': "<p>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)</p>\n"}, {'answer_id': 15236, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 4, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 15243, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 4, 'selected': False, 'text': "<blockquote>\n  <p>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.</p>\n</blockquote>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>At least prompt them with 'are you sure' rather than flat out preventing it.</p>\n"}, {'answer_id': 49349, 'author': 'Matt Warren', 'author_id': 4500, 'author_profile': 'https://Stackoverflow.com/users/4500', 'pm_score': 6, 'selected': False, 'text': '<p>If you look at the <a href="https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.closereason?view=netframework-4.7.2" rel="noreferrer">value</a> of <code>FormClosingEventArgs e.CloseReason</code>, it will tell you why the form is being closed. You can then decide what to do, the possible values are:</p>\n\n<p><strong>Member name</strong> - Description</p>\n\n<hr>\n\n<p><strong>None</strong> - The cause of the closure was not defined or could not be determined.</p>\n\n<p><strong>WindowsShutDown</strong> - The operating system is closing all applications before shutting down.</p>\n\n<p><strong>MdiFormClosing</strong> - The parent form of this multiple document interface (MDI) form is closing.</p>\n\n<p><strong>UserClosing</strong> - 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 <kbd>ALT</kbd>+<kbd>F4</kbd>.</p>\n\n<p><strong>TaskManagerClosing</strong> - The Microsoft Windows Task Manager is closing the application.</p>\n\n<p><strong>FormOwnerClosing</strong> - The owner form is closing.</p>\n\n<p><strong>ApplicationExitCall</strong> - The Exit method of the Application class was invoked.</p>\n'}, {'answer_id': 428005, 'author': 'antsyawn', 'author_id': 53324, 'author_profile': 'https://Stackoverflow.com/users/53324', 'pm_score': 5, 'selected': False, 'text': '<p>I believe this is the right way to do it:</p>\n\n<pre><code>protected override void OnFormClosing(FormClosingEventArgs e)\n{\n  switch (e.CloseReason)\n  {\n    case CloseReason.UserClosing:\n      e.Cancel = true;\n      break;\n  }\n\n  base.OnFormClosing(e);\n}\n</code></pre>\n'}, {'answer_id': 15216009, 'author': 'linquize', 'author_id': 1031218, 'author_profile': 'https://Stackoverflow.com/users/1031218', 'pm_score': 2, 'selected': False, 'text': '<p>Subscribe FormClosing event</p>\n\n<pre><code>private void Form1_FormClosing(object sender, FormClosingEventArgs e)\n{\n    e.Cancel = e.CloseReason == CloseReason.UserClosing;\n}\n</code></pre>\n\n<p>Only one line in the method body.</p>\n'}, {'answer_id': 37909195, 'author': 'Bharath theorare', 'author_id': 2700841, 'author_profile': 'https://Stackoverflow.com/users/2700841', 'pm_score': -1, 'selected': False, 'text': '<p>Hide close button on form by using the following in constructor of the form:</p>\n\n<pre><code>this.ControlBox = false;\n</code></pre>\n'}, {'answer_id': 47331474, 'author': 'Brahim Bourass', 'author_id': 8947184, 'author_profile': 'https://Stackoverflow.com/users/8947184', 'pm_score': 3, 'selected': False, 'text': '<p>This is a hack to disable <kbd>Alt</kbd> + <kbd>F4</kbd>.</p>\n\n<pre><code>private void test_FormClosing(object sender, FormClosingEventArgs e)\n{\n    if (this.ModifierKeys == Keys.Alt || this.ModifierKeys == Keys.F4) \n    { \n        e.Cancel = true; \n    }    \n}\n</code></pre>\n'}, {'answer_id': 50615223, 'author': 'Condorkunka', 'author_id': 5999286, 'author_profile': 'https://Stackoverflow.com/users/5999286', 'pm_score': 2, 'selected': False, 'text': '<p>This does the job:</p>\n\n<pre><code>bool myButtonWasClicked = false;\nprivate void Exit_Click(object sender, EventArgs e)\n{\n  myButtonWasClicked = true;\n  Application.Exit();\n}\nprivate void Form1_FormClosing(object sender, FormClosingEventArgs e)\n{\n  if (myButtonWasClicked)\n  {\n    e.Cancel = false;\n  }\n  else\n  {\n    e.Cancel = true;\n  }\n\n\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/14943', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1375/']