disable ALT+F4

G

Guest

I have written a small program and I want the user to close it down solely by
clicking on a button. How can I disable the ALT+F4 method of closing the
application down?
 
Y

Yves Dhondt

You should trap the Closing event of your main form. Use some boolean to
check if your button was clicked or not.

bool myButtonWasClicked = false;

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
// Only allow closing if your button was clicked
if (!myButtonWasClicked)
e.Cancel = true;
}

HTH

Yves
 
G

Guest

Hi!

If you will block the form's "Closing" then you'll
have problem with killing its thread.

I've got much more "safe" proposition for you.
I have written a small program and I want the user to close it down solely by
clicking on a button. How can I disable the ALT+F4 method of closing the
application down?

You can handle "Alt+F4" KeyDown to disable it.

1. set "KeyPreview" of your form to "true"

2. add "KeyDown" handler for your form (or override "OnKeyDown" method)

private void Form1_KeyDown(object sender
, System.Windows.Forms.KeyEventArgs e) {
if( e.Alt && e.KeyCode==Keys.F4 ) {
e.Handled=true;
}
}

Cheers!

Marcin
 

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