BuZz
BuZz

Reputation: 17455

C# what event to catch UserControl disposed of?

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 ?

Upvotes: 4

Views: 12031

Answers (2)

Dave
Dave

Reputation: 860

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.

Upvotes: 13

Justin
Justin

Reputation: 86729

It sounds like the Disposed event is what you are looking for.

Upvotes: 7

Related Questions