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

后端 未结 2 2002
梦如初夏
梦如初夏 2020-12-25 10:58

When you create a new form in Visual Studio, the designer generates the following code in the .Designer.cs file:

  /// 
  /// Required designe         


        
2条回答
  •  离开以前
    2020-12-25 11:14

    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.

提交回复
热议问题