2

I've read a few articles about disabling Alt+F4 and so far, have got it part functioning.

Currently, I have this on my main form, and it does a great job of only allowing the form to be closed when I need it to..

private void Alerter_FormClosing(object sender, FormClosingEventArgs e)
{
    if (_altF4Pressed)
    {
        if (e.CloseReason == CloseReason.UserClosing)
            e.Cancel = true;
        _altF4Pressed = false;
    }
}

private void Alerter_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Alt && e.KeyCode == Keys.F4)
        _altF4Pressed = true;
}

The problem however, is that this does not work when I display a dialog. The form still closed on Alt+F4, and it's imperative that this does not happen as gives the user a way to circumvent process and procedure.

Currently, my alert form is called using;

    public static DialogResult show(string param)
    {
        thisParam = param;

        using (Alerter alert = new Alerter())
        {
            if (alert.ShowDialog() == DialogResult.OK) { return DialogResult.OK; }
            else { return DialogResult.Cancel; }
        }
    }

and my main form calls it using this;

Alerter.show(rxLine);

How can I prevent the new form from being closed at all, unless I specifically tell it to?

It'sNotALie.
  • 22,289
  • 12
  • 68
  • 103
Frankish EQG
  • 114
  • 7
  • 2
    Why on EARTH would you **EVER** want this? ALT+F4 is used as a last resort. Whaat are you writing, a virus? – It'sNotALie. May 03 '13 at 22:30
  • 1
    Have to agree with @ofstream: There are almost no circumstances where it's appropriate to do this to your users. If they want your dialog out of the way, they *will* get it out of the way somehow. If you offer them the ability to close (along with an "Are you sure?" dialog) then they'll use that. If you *don't*, then they'll be annoyed with you, and then use the task manager to kill the process anyway. – dlev May 03 '13 at 22:46
  • While I can understand the cautiousness.. no, I'm not writing a virus... far from it actually. I'm writing an upgrade to an existing bespoke application, the purpose of which is to notify the end user of product recalls and enforce acknowledgement of the notification. The signee is then responsible for actioning the recall.. We also build the machines the users use.. and lock down unwanted access to prevent critical processes such as databases from being tampered with. – Frankish EQG May 04 '13 at 00:04

1 Answers1

7

From How can I prevent a user from closing my C# application?, you can just do this:

protected override CreateParams CreateParams
{
   get
   {
      const int CS_NOCLOSE = 0x200;

      CreateParams cp = base.CreateParams;
      cp.ClassStyle |= CS_NOCLOSE;
      return cp;
   }
}

No need to track the keys or cancel the closing in the FormClosing event. Also the close button in the corner will be disabled, but you can still close the form with this.Close();

Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225