简体   繁体   中英

Are custom Winforms control auto generated designer files needed?

Visual Studio creates code similar to this:

namespace MyCustomControls
{
    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.

严格来说,我不这么认为,但是那样您将失去创建控件时将获得的任何设计时间收益,而我的猜测是VS将继续尝试重新创建它们。

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