Reputation: 2258
What is the best practice for implementing IDisposable on a Winform?
Ideally I would be able to hook into the Dispos(bool) override in the generated code to dispose the manually added IDisposable object. Any recommendations on how to do this properly?
Thanks.
Scott
Upvotes: 1
Views: 1158
Reputation: 2757
I unregister any events in the Dispose method found in the form's designer.cs
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
_frmFind.RaiseFindEvent -= _frmFind_RaiseFindEvent;
base.Dispose(disposing);
}
Upvotes: 1
Reputation: 4703
You can move that Dispose implementation out of the .designer.cs and in your .cs.
Upvotes: 5
Reputation: 56944
But, then your field needs to be a Component (implement the IComponent interface or something similar). Wouldn't that be a little overkill ?
Maybe you can attach an eventhandler to the Disposing event, and dispose your fields in that eventhandler ?
(Or just add them to the Dispose method - I don't think it will be a problem, since afaik, the code in the Dispose method is not regenerated ... Ideally, the Dispose method implementation shouldn't have been in the *.designer.cs class ... ).
Upvotes: 2