e-holder e-holder -4 years ago 72
C# Question

How do I add Dispose functionality to a C# UserControl?

I have a class which implements UserControl. In .NET 2005, a Dispose method is automatically created in the MyClass.Designer.cs partial class file that looks like this:

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}


If I want to add my own Dispose functionality, where would I put it? Since this file is generated, I don't want to add code here and risk it getting blown away.

Answer Source

In such a case I move the generated Dispose method to the main file and extend it. Visual Studio respects this.

An other approach would be using a partial method (C# 3.0).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download