是否需要自定义Winforms控件自动生成的设计器文件?

时间:2009-11-18 22:57:03

标签: c# .net winforms

Visual Studio创建类似于此的代码:

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
    }
}

4 个答案:

答案 0 :(得分:3)

如果您使用设计器表面来布局子控件等(在设计器中非常多任何),自定义设计器文件是必不可少的。如果您那么这样做(例如,您正在使用自定义绘画,或手动添加子控件),那么您通常可以非常安全地杀死此文件。

在某些方面,这可以更轻松地实现Dispose()等。

答案 1 :(得分:1)

不,不需要从创建新自定义控件的初始吐出生成的代码。它没有做任何功能,基本上可以从你的应用程序中注释掉。

但是,当您在设计器中对自定义控件执行任何操作时,将会在设计器文件中生成非常重要和有用的代码。比如创建任何添加的控件,事件处理程序连接以及基本的初始化和布局。此代码无法删除。

答案 2 :(得分:1)

您可以使用CustomControl模板获取这些文件,部分类设置和设计图面。

您还可以通过添加“新类”来启动控件,并仅添加您真正需要的内容。如果您不使用设计器(并且您通常不使用CustomControl),那么只能使用它。

答案 3 :(得分:0)

严格来说,我不这么认为,但是在创建控件时你将失去任何设计时间的好处,我的猜测是VS将继续尝试重新创建它们。

相关问题