What's the purpose of the components IContainer generated by the Winforms designer?

这一生的挚爱 提交于 2019-11-30 04:45:25

When you add non-UI components to the form (such as a Timer component), components will be the parent of those compoments. The code in the designer file makes sure that these components are disposed of when the form is disposed. If you have not added any such components to the form in design time, components will be null.

Since components is designer generated, and will be null if you have no non-UI compoments on the form (in design time), I would personally opt for managing those components in some other way, disposing them on form close or something like that.

The components variable is the equivalent of the form's Controls variable. Which keeps track of all the controls put on a form. So that a form can automatically dispose all the controls when it is closed, a very important clean-up duty.

The form class has no equivalent member that keeps track of all the Components that were dropped on it at design time so the designer takes care of it automatically.

Note that moving the Dispose() method from the Designer.cs file to the main form source code file is quite acceptable. I strongly recommend you do so, no reason to make the Form class 'special' in any way, it is just a managed class like any other. Add Dispose() calls to dispose members as needed before the base.Dispose call.

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