tom
tom

Reputation: 5001

form close event in c#

I am trying to do some cleaning up when a form closes. I am using the following signature

private void BatchGui_Closing(object sender, FormClosingEventArgs e)

The issue is, if i put a breakpoint in there the code never executes, so how do i properly write a method for a form close event?

Thanks

Upvotes: 1

Views: 8762

Answers (3)

Øyvind Bråthen
Øyvind Bråthen

Reputation: 60744

I guess you have linked this method to the forms Closing event?

myForm.FormClosing += BatchGui_Closing;

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942328

Sounds to me you've forgotten to set the event handler. Lightning icon in the Properties window.

Best thing to do in general is to override the OnXxx method of the form, you don't need an event to listen to your own class object's events. Type this:

  protected override

and the IntelliSense window pops up to let you pick OnFormClosing. After which it should look like this:

    protected override void OnFormClosing(FormClosingEventArgs e) {
        // insert your code here
        // ...
        base.OnFormClosing(e);
    }

Upvotes: 4

Emond
Emond

Reputation: 50692

In WinForms the event is called FormClosing:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{

}

Make sure you attached the event in the designer. Just adding the method is not enough!

Upvotes: 4

Related Questions