Randy Voet
Randy Voet

Reputation: 3830

Custom Dispose in designed form or user control

I've created a custom WinForms UserControl and would like to override Dispose. However the code generated by the designed already contains a Dispose method. How can I add custom cleanup code to my component?

Upvotes: 8

Views: 2203

Answers (4)

Randy Voet
Randy Voet

Reputation: 3830

I just realized that the Dispose method in the x.Designer.cs is outside the 'Component Designer generated code' region. So simply moving the Dispose method to my normal 'code behind' solves my problem.

Upvotes: 2

Henk Holterman
Henk Holterman

Reputation: 273691

It's badly documented but you can Cut & Paste the Dispose method over to your side of the partial class.
And then extend it.

Upvotes: 2

Stefan P.
Stefan P.

Reputation: 9519

You can move the Dispose method from the code generated file into your control cs file. I've used this under .net 2.0, it should work on 4.0 as well.

Upvotes: 3

dexter
dexter

Reputation: 7213

Depending on what kind of resources you want to dispose of you also could use finalizer (destructor) to achieve that as well.

Upvotes: 0

Related Questions