c# WinForms form still closes after setting e.Cancel = true in Form_FormClosing event
Asked Answered
D

1

6

This is the code in question:

    private void FormAccounting_FormClosing(object sender, FormClosingEventArgs e)
    {
        Properties.Settings.Default.FormAccountingLocation = this.Location;
        Properties.Settings.Default.Save();
        if (IsEditing)
        {
            MessageBox.Show("Please save or cancel open transactions before closing the accounting window.", "Open Transactions", MessageBoxButtons.OK, MessageBoxIcon.Information);
            e.Cancel = true;
        }
    }

I've added breakpoints to the e.Cancel = true; line to ensure it's being executed.

The form closes immediately after clicking Ok.

Here's the code that calls FormAccounting:

    private void buttonAccounts_Click(object sender, EventArgs e)
    {
        FormAccounting NewFormAccounting = new FormAccounting();
        NewFormAccounting.Show();
    }
Dredger answered 17/8, 2011 at 11:14 Comment(10)
Try putting e.Cancel After the message boxes. I have the same and e.Cancel is the last lineIggie
Your code works fine for me. Is "FormAccounting" your main form?Mallorca
Remove the MessageBox. It's a bad way of debugging/tracing anyway.Mustard
No, its called from FormMain using .Show(), and @Henk, done.Dredger
Could it be that there is another event handler also handling the FormClosing event, and that sets e.Cancel to true?Darceydarci
I've not been able to find any other event handlers for this event in FormAccount.cs or FormAccounting.designer.csDredger
@fenix: how about the main form (or wherever FormAccount is instantiated)?Darceydarci
@Dredger can you describe how you close the form? It could also be that the form is being disposed, which would cause it to close.Fanlight
@Dredger please amend your question to provide more code. The code presented in your sample in isolation works, so something else is the problem here.Fanlight
@fredrik, i've added that code to the question.Dredger
G
13

Canceling the form close event works to prevent:

  1. User closing the form
  2. Application.Exit from exiting the application
  3. Code from calling Form.Close on the form

But it does not work to prevent:

  1. User closing application's main form
  2. Code calling Form.Dispose on the form
  3. Code calling Form.Close on the application's main window

The last 3 cases don't even trigger the form close event on the non-main form, so the form goes away without a chance to cancel it. Perhaps your application is causing the form to first close in one of the first 3 ways, which triggers the event, and then in one of the second 3 ways (or something similar), which does not trigger the event and forces the form closed anyway.

Edit: Add this function to your form's code and it will allow you to review in the debugger what the call stack looks like when your window is getting closed so you can see what is actually causing it:

  protected override void DestroyHandle()
  {
     System.Diagnostics.Debugger.Break();
     base.DestroyHandle();
  }
Giliana answered 17/8, 2011 at 11:38 Comment(1)
Yeah there was a stray this.Dispose() in the close button's click event. thanks.Dredger

© 2022 - 2024 — McMap. All rights reserved.