Dispose on user controls, really meant to edit the .designer.cs file?

你离开我真会死。 提交于 2019-12-01 21:41:15

If you're talking about WinForms I usually take one of two approaches to solve this problem.

Approach 1

Open the Form.Designer.cs file. Inside the generated dispose method I add a call to DisposeCore. I then go back to Form.cs and add a DisposeCore method that will now be called during dispose. I add all of my dispose logic into this method.

Editing the designer file is technically not supported. However I've found that this particular edit will not be washed away when the designer regenerates code.

Approach 2

Add a event handler to Form.Disposed and do my dispose logic in the handler. This is the preferable way because it's a supported operation and won't be affected by some designer generation you have yet to encounter.

Could you clarify what kind of controls? ASP.NET, WinForms?

In ASP.NET you could:

protected override void OnUnload(EventArgs e){
     base.OnUnload(e);
     //Unload here...
}

Or, you could cut and paste it over to the main .cs file. That code is not inside of "#region Component Designer generated code", so Studio won't miss it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!