Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Disable Alt + F4 closing form?

Tags:

c#

.net

winforms

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.

like image 803
Ryan Sampson Avatar asked Aug 18 '08 17:08

Ryan Sampson


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 .


1 Answers

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(); 
like image 159
Martin Avatar answered Oct 16 '22 03:10

Martin