Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# what event to catch UserControl disposed of?

Tags:

c#

winforms

What would be the event to catch when a UserControl is disposed of in C# ? I'd like to catch it to do some clean up, but after viewing the list of events available in the designer, it seems there is no such thing ?

like image 297
BuZz Avatar asked Dec 02 '22 00:12

BuZz


1 Answers

When you create the user control a Dispose method is created automatically for you in the yourUserControlName.Designer.cs file. Add whatever clean up code that method. You may want to change the auto generated code to something like this:

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
            // your clean up code here
        }
        base.Dispose(disposing);
    }

That way your clean up code will not be dependent on the components object.

like image 145
Dave Avatar answered Dec 21 '22 06:12

Dave