简体   繁体   中英

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?

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.

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

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.

根据要处理的资源类型,也可以使用终结器(析构函数)来实现这一点。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM