开发者

Are custom Winforms control auto generated designer files needed?

开发者 https://www.devze.com 2022-12-12 01:30 出处:网络
Visual Studio creates code similar to this: namespace MyCustomContr开发者_StackOverflow中文版ols

Visual Studio creates code similar to this:

namespace MyCustomContr开发者_StackOverflow中文版ols
{
    partial class MyCustomControl
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose ( bool disposing )
        {
            if ( disposing && ( components != null ) )
            {
                components.Dispose ( );
            }
            base.Dispose ( disposing );
        }

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent ( )
        {
            components = new System.ComponentModel.Container ( );
        }

        #endregion
    }
}


The custom designer file is essential if you are using the designer surface to lay out child controls etc (pretty-much anything in the designer). If you aren't doing then this (for example, you are using custom painting, or adding the child controls manually), then you can usually kill this file pretty safely.

In some ways this makes it easier to implement Dispose() etc.


No, the code which is generated from the initial spit of creating a new custom control is not needed. It doesn't do anything functional and can essentially be commented out of your application.

However, the moment you do anything with your custom control in the designer, very important and useful code will be generated into the designer file. Such as the creation of any added controls, event handler hookup and basic initialization and layout. This code cannot be deleted.


You get those files, the partial class setup and Design surface because you started with the CustomControl Template.

You can also start a Control by adding a 'New Class' and add only the stuff you really need. If you don't use the designer (and you usually don't for a CustomControl) then having it only gets in the way.


Strictly speaking I don't think so, but then you will lose any design time benefit you would get when creating the control, and my guess is that VS will keep trying to recreate them.

0

精彩评论

暂无评论...
验证码 换一张
取 消