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

N

Nikki

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

Guest

You could Handle the Closing event th following way:

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

Mark Broadbent

....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.
 
M

Mark Broadbent

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!
 
M

Morten Wennevik

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.
 
M

Markus Stoeger

Mark said:
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
 
M

Mark Broadbent

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top