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