4

This is not a duplicate of How to Disable Alt + F4 closing form?. Please read why.

I have made a custom MessageBox under my main Form.

enter image description here

And have set "Aight" button click listener as:

private void Aight_buton_Click(object sender, EventArgs e)
{
    dr = DialogResult.OK;
    Close();
}

The same is the case with the "X" button. Following the above question's answer I could have done this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = e.CloseReason == CloseReason.UserClosing;
}

but since I am using Close() under the Aight_buton_Click it still registers as e.CloseReason == CloseReason.UserClosing;. So hitting the key doesn't close my form (a custom messagebox) nor does Alt+F4. I would like to know how specifically I can prevent only Alt+F4 closes and not the Close() closes. And please I would rather not use ModifierKeys as it is not the most appropriate not the smartest way to handle this situation.

Rishav
  • 3,818
  • 1
  • 31
  • 49
  • You can use an additional flag, no? – TaW May 21 '18 at 20:19
  • @TaW I'm more or less new to C#. I'd be glad if you could elaborate. An additional flag checking what exactly? – Rishav May 21 '18 at 20:22
  • 1
    Create a class level `bool doClose = false;` You set it to true in the button click and test it in closing. – TaW May 21 '18 at 20:24
  • I am pretty sure that unless you repurpose the Alt+F4 key sequence, it's always going to generate the close message. – jwdonahue May 21 '18 at 20:24

3 Answers3

6

Handle Atl+F4 by your self and set it handled.

In the form constructor first set

this.KeyPreview = true;

Then handle the keyDown event

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

 }
Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
  • 1
    Thanks man. I've seen many answers in other forums as well but all answers talk about a workaround making the buttons close the form just like Zohar's answer but only this answer prevents Alt + F4 closing. – Rishav May 22 '18 at 02:22
5

Add a bool field to the form, set it to true in the Aight_buton_Click method, and in the Form1_FormClosing only prevent the form from being closed if that field is false.

So on the form level:

private bool _isAightButonClicked;

Set it to true in the Aight_buton_Click method:

private void Aight_buton_Click(object sender, EventArgs e)
{
    _isAightButonClicked = true;
    dr = DialogResult.OK;
    Close(); 
}

Use it in the Form1_FormClosing method:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    e.Cancel = e.CloseReason == CloseReason.UserClosing && !_isAightButonClicked;
}
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Zohar Peled answer is correct to prevent closing form with `boolean` and `Form1_FormClosing` event but since i needed window that couldn't be closed i assume you do not want to let user close it in any way so other problem you will have is that user could close window from Task Manager. Solution to this problem is to add handler for `Form1_FormClosed` So if form is closed (somehow) and your `boolean == false` create new form again. This way i prevented user to close it any way. – Aleksa Ristic May 22 '18 at 00:44
0

Try this, it works great:

Private Sub FrmMikeStats_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

        'Stop Alt-F4 from closing this program ***************************************************************************
        '*****************************************************************************************************************
        If ModifierKeys = Keys.Alt Or ModifierKeys = Keys.F4 Then
            e.Cancel = True
        End If
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 24 '21 at 02:59