24

How can I get my windows form to do something when it is closed.

sooprise
  • 22,657
  • 67
  • 188
  • 276

6 Answers6

36

Handle the FormClosed event.

To do that, go to the Events tab in the Properties window and double-click the FormClosed event to add a handler for it.

You can then put your code in the generated MyForm_FormClosed handler.

You can also so this by overriding the OnFormClosed method; to do that, type override onformcl in the code window and OnFormClosed from IntelliSense.

If you want to be able to prevent the form from closing, handle the FormClosing event instead, and set e.Cancel to true.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • there is no "Closed" event, only "FormClosed" - quite unintuitive – hello_earth May 31 '12 at 12:24
  • 2
    @hello_earth: There was a `Closed` event in .Net 1; it was replaced in .Net 2.0 due to various issues. The original `Closed` event is still there for compatibility, but is hidden. – SLaks May 31 '12 at 12:55
16

Or another alternative is to override the OnFormClosed() or OnFormClosing() methods from System.Windows.Forms.Form.

Whether you should use this method depends on the context of the problem, and is more usable when the form will be sub classed several times and they all need to perform the same code.

Events are more useful for one or two instances if you're doing the same thing.

public class FormClass : Form
{
   protected override void OnFormClosing(FormClosingEventArgs e)
   {
        base.OnFormClosing(e);
        // Code
   } 
}
MovGP0
  • 7,267
  • 3
  • 49
  • 42
Ian
  • 33,605
  • 26
  • 118
  • 198
  • 7
    **And call `base.OnFormClosing(e)`.** – SLaks May 25 '10 at 16:18
  • 1
    I would do that naturally and considered it part of the //Code comment. But indeed you're correct, probably should be in there for the lesser experienced. Thanks SLaks. – Ian May 26 '10 at 08:04
10

WinForms has two events that you may want to look at.

The first, the FormClosing event, happens before the form is actually closed. In this event, you can still access any controls and variables in the form's class. You can also cancel the form close by setting e.Cancel = true; (where e is a System.Windows.Forms.FormClosingEventArgs sent as the second argument to FormClosing).

The second, the FormClosed event, happens after the form is closed. At this point, you can't access any controls that the form had, although you can still do cleanup on variables (such as Closing managed resources).

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 2
    Updated this to use `FormClosing` and `FormClosed` as `Closing` and `Closed` are deprecated. – Powerlord May 25 '10 at 15:24
  • Is it definitely "all variables" as well as controls on the form? I tried just now and referenced 2 fields of the Form class I have (inherited from Form) and they could be accessed fine - in the FormClosed event – PandaWood Sep 19 '14 at 06:16
  • @PandaWood Actually, you're right. It's just controls that are inaccessible at this point. I'll fix it in this answer. – Powerlord Sep 19 '14 at 13:33
4

Add an Event Handler to the FormClosed event for your Form.

public class Form1
{

    public Form1()
    {    
        this.FormClosed += MyClosedHandler;
    }

    protected void MyClosedHandler(object sender, EventArgs e)
    {
        // Handle the Event here.
    }
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2
 public FormName()
 {
      InitializeComponent();
      this.FormClosed += FormName_FormClosed;
 }

private void FormName_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
{
   //close logic here
}
mint
  • 3,341
  • 11
  • 38
  • 55
-3

Syntax :

   form_name.ActiveForm.Close();

Example:

   {
         Form1.ActiveForm.close();
   }
Baby Groot
  • 4,637
  • 39
  • 52
  • 71