Share via


C#: ALT+F4

Question

Wednesday, July 17, 2013 5:28 AM

Hi,

Can anyone help me how can I not allow the Windows Application to not close when ALT+F4 is pressed?

Thanks,

MukuroRokudo

All replies (8)

Wednesday, July 17, 2013 5:52 AM âś…Answered | 1 vote

As "Alt+F4" is the default for closing a window/application i wouldn`t change the default behaviour. If your users are the typical windows user, they will be used to closing an application ( or window ) by pressing Alt+F4.

If you still want do it, you can handle the FormClosing event and check the CloseReason property of the CloseFomrEventArgs like:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == System.Windows.Forms.CloseReason.UserClosing)
            {
                e.Cancel = true;
            }
        }

Hannes

If you have got questions about this, just ask.

In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.

My Forum Threads


Wednesday, July 17, 2013 6:49 AM

Hi MukuroRokudo,

http://www.codeproject.com/Questions/225922/How-do-I-Disable-AltplusF4-Key-from-closing-my-app

http://stackoverflow.com/questions/2643712/c-sharp-disable-altf4-but-allow-the-the-form-to-be-closed-by-code-closereason

http://stackoverflow.com/questions/12898680/how-to-disable-altf4-for-the-application

http://stackoverflow.com/questions/14943/how-to-disable-alt-f4-closing-form


Wednesday, July 17, 2013 6:50 AM

Thanks! I need to disable it because I have an exit button to close the application. If the user use the ALT+F4 or X button then I can't limit the application that they will complain about unsaved changes they made are lost. So I created the exit button so that's the only place where they can exit or by forcing the application in process to end task and if they did so that's not my fault anymore :)


Wednesday, July 17, 2013 12:04 PM | 1 vote

I am not certain you entirely udnerstood it, but all your extra work with a seperate Closing button is unessesary:

No matter how you try to close a form (clicking the Close button, this.Close(), Ctrl+F4 in some Programms) the Closing Event will be called wich allows you to cancel the Closing of the Form.

ALT+F4 will propably (95%) just call Application.Exit(). Wich will first raise the Closing event of every visible form. If even one of them cancel's, it will abort the exit of the application completely.

So all you need is to write a proper Closing event for whatever form is used to edit the data. Aside from killing the Programm in TaskManager or a hard shutdown there will be no way around the Event. And those you cannot affect anyway with software (and shut not try too, your software could have a bug preventing closure after all).

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Wednesday, July 17, 2013 12:12 PM

Thanks! I need to disable it because I have an exit button to close the application. If the user use the ALT+F4 or X button then I can't limit the application that they will complain about unsaved changes they made are lost. So I created the exit button so that's the only place where they can exit or by forcing the application in process to end task and if they did so that's not my fault anymore :)

Why don't you handle the FormClosing event as Heslacher suggested then run your exit button logic, which presumably saves the user data?  That would be far preferable to blocking the close from happening.


Wednesday, October 21, 2015 2:04 PM

Hello,

It does not work for me, alt+f4 still works :(

or did I put the code in the wrong place?

I dont understaaaaaaaaaaaaaaaaaaaaaaaaaand :'c

Sincerely,

William Holmstrand


Wednesday, October 21, 2015 2:55 PM

Did you follow Heslacher's example? You already marked his response as an answer so might be best to log a new thread if you are having an issue implementing the Form Closing event.


Wednesday, October 21, 2015 4:31 PM

Did you follow Heslacher's example? You already marked his response as an answer so might be best to log a new thread if you are having an issue implementing the Form Closing event.

It's an other user as far as I can see ;-)

@MailliWilliam: did you link the provided method to the FormClosing event?