I want that the form will not close by doing Alt + F4 but if Application.Exit()
or this.Close
is called from the same Form, it should be closed.
I tried CloseReason.UserClosing
but still no help.
I want that the form will not close by doing Alt + F4 but if Application.Exit()
or this.Close
is called from the same Form, it should be closed.
I tried CloseReason.UserClosing
but still no help.
It's very easy you can do it by set SuppressKeyPress property to true on Form_Keydown EventHandler as below.
if (e.KeyCode == Keys.F4 && e.Alt)
{
e.SuppressKeyPress = true;
}
With this you can also close your active form by set SuppressKeyPress Property to false on same eventHandller or any other way.
Capture Alt+F4 hotkey by setting Form's KeyPreview property to true and overriding OnProcessCmdKey method.
How did you use CloseReason?
See the sample code here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx
You need to set the Cancel property of the passed FormClosingEventArgs object to stop the form closing.
If you need to filter out Alt + F4 event only (leaving clicking of close box,
this.Close()
andApplication.Exit()
to behave as usual) then I can suggest the following:KeyPreview
property totrue
;Wire up form's
FormClosing
andKeyDown
events: