By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 968 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

i want to prevent windows to close my form when Alt+f4 is pressed

P: n/a
Hi,
Can anybody help me, i want to prevent windows to close my winform of
..NET application, when user presses Alt+F4

Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
You could Handle the Closing event th following way:

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
}

"Nikki" wrote:
Hi,
Can anybody help me, i want to prevent windows to close my winform of
..NET application, when user presses Alt+F4

Nov 17 '05 #2

P: n/a
....Great that prevents the close (I might add *always* too!) but hardly
helps Nikki with the solution, of which the crux of it is detecting the key
events in the first place.

***Nikki*** There are possibly more elegant ways to do this (I can think of
at least one other) but they will all essentially use the following
detection. Your form will have one private var that indicates whether the
form should be allowed to close or not. You detect the press and set this
var to disallow. Then in the forms (because it will still fire) closing
event you should read this var, and allow close or not. Obviously if close
is not allowed and doesnt happen then the var should then be reset to allow
close for any future events.

The code snippet is as follows (if you want me to include the class file
then let me know). Might I also suggest that rather than preventing the
close you should handle the close i.e. provide a prompt to the user and if
they say OK then cleanup. The reason I say this is because ALT+F4 is (as you
know) known and expected behaviour -and you are taking that away (IMHO I
would say that is poor design). Personally it would annoy the hell out of
me.

private bool enableClose = true;

private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F4 && e.Alt)
{
MessageBox.Show("Sorry you cannot close this form via ALT+F4");
enableClose = false;
}
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (enableClose == false)
{
enableClose = true;
e.Cancel = true;
}
}

br,

Mark.
"Marinus Holkema" <Ma************@discussions.microsoft.com> wrote in
message news:74**********************************@microsof t.com...
You could Handle the Closing event th following way:

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
e.Cancel=true;
}

"Nikki" wrote:
Hi,
Can anybody help me, i want to prevent windows to close my winform of
..NET application, when user presses Alt+F4

Nov 17 '05 #3

P: n/a


Thank you Mark. It's a gr8 help, U saved lot of my time & effort.
*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4

P: n/a
Absolutely no problem at all :-)
In fact you might be able to help me, if you know of any C# jobs in my
vacinity (Cambs UK) then let me know....I'm on the hunt!

"Nikki V" <ap*********@gmail.com> wrote in message
news:e8**************@TK2MSFTNGP12.phx.gbl...


Thank you Mark. It's a gr8 help, U saved lot of my time & effort.
*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #5

P: n/a
The problem with this solution is that it prevents windows closing the program when it shuts down as well, and thereby prevents shutting down the computer.

You can detect what triggered the Closing event by parsing the 7th last StackFrame

protected override void OnClosing(CancelEventArgs e)
{
// frames 1-6 will be identical
StackTrace st = new StackTrace(7);
StackFrame sf = st.GetFrame(0); // 7th frame

switch(sf.GetMethod().Name)
{
case "DefWndProc":
// user pressed Alt-F4 or clicked the X button
break;
case "SendMessage":
// something in the code called Close()
break;
}
}

Another problem with this is that Windows itself uses DefWndProc for closing programs at ShutDown as well. You need to set a flag in DefWndProc that you can use inside the Closing event.

The procedure for this is as follows:

private bool shutdown = false;
protected override void DefWndProc(ref Message m)
{
const int WM_QUERYENDSESSION = 0x0011;
if(m.Msg == WM_QUERYENDSESSION)
shutdown = true;
base.DefWndProc (ref m);
}

Inside the Closing event, if shutdown is true, don't cancel the closing event.

In theory this should work, but in my test cases Windows never sent the WM_QUERYENDSESSION message. It may work for you though.
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #6

P: n/a
Mark Broadbent wrote:
private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F4 && e.Alt)
{
MessageBox.Show("Sorry you cannot close this form via ALT+F4");
enableClose = false;
}
}


Don't you have to set KeyPreview = true on the form for this to work?
Otherwise the KeyDown event gets fired in the "control-with-focus" and the
form doesn't see it but gets closed anyway.

Another idea would be to set e.Handled = true in the KeyDown event when you
detect ALT-F4. This way the key simply gets ignored and you don't need to
check enableClose in the Closing method.

Just my 2c.. I needed the same just a few days ago.

Max

Nov 17 '05 #7

P: n/a
Your post is of course completely correct, well spotted! Wrote my code in a
few seconds so missed those two things ;-)

"Markus Stoeger" <sp******@gmx.at> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Mark Broadbent wrote:
private void Form1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F4 && e.Alt)
{
MessageBox.Show("Sorry you cannot close this form via ALT+F4");
enableClose = false;
}
}


Don't you have to set KeyPreview = true on the form for this to work?
Otherwise the KeyDown event gets fired in the "control-with-focus" and the
form doesn't see it but gets closed anyway.

Another idea would be to set e.Handled = true in the KeyDown event when
you
detect ALT-F4. This way the key simply gets ignored and you don't need to
check enableClose in the Closing method.

Just my 2c.. I needed the same just a few days ago.

Max

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.