InitializeComponent和Dispose函数 [英] InitializeComponent And Dispose Function

查看:73
本文介绍了InitializeComponent和Dispose函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在一些项目中(在Code Project和其他项目中),开发人员将InitializeComponent和Dispose移动到Form Code(把它放在表单中)



他们为什么这样做?以及如何从这一举措中获益?





例如:



[ ] [ Sticky Windows ]



表格1





使用System;使用System.Drawing 
;
使用System.Collections;使用System.ComponentModel
;
使用System.Windows.Forms;
使用System.Data;

命名空间WindowsApplication1
{
/// < summary >
/// Form1的摘要说明。
/// < / summary > ;
公共类Form1:System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
/// // < 摘要 >
///所需的设计变量。
/// < / summary > ;
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows Form Designer支持需要
//
InitializeComponent();

//
// TODO:在InitializeComponent调用后添加任何构造函数代码
//

//Blue.Windows.StickyWindow.RegisterExternalReferenceForm(this );
}

/// < 摘要 >
///清理正在使用的所有资源。
/// < / summary > ;
protected override void Dispose(bool disposing)
{
if(disposing)
{
if(components!= null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}

#region Windows窗体设计器生成的代码
/// < 摘要 >
/// Designer支持所需的方法 - 不要修改
///的内容这个方法用代码编辑器。
/// < / summary > ;
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(10,16);
this.button1.Name =button1;
this.button1.Size = new System.Drawing.Size(102,23);
this.button1.TabIndex = 0;
this.button1.Text =新测试表格;
this.button1.Click + = new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5,13);
this.ClientSize = new System.Drawing.Size(350,53);
this.Controls.Add(this.button1);
this.Name =Form1;
this.Text =Form1;
this.ResumeLayout(false);

}
#endregion

/// < 摘要 >
///应用程序的主要入口点。
/// < / summary > ;
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender,System.EventArgs e)
{
Form2 ff = new Form2();
ff.Show();
}
}
}











表格2:







使用System;

使用System.Drawing;

使用System.Collections;

使用System .ComponentModel;

使用System.Windows.Forms;



使用Blue.Windows;



名称空间WindowsApplication1

{

///< summary>

/// Form2的摘要说明。

///

公共类Form2:System.Windows.Forms.Form

{

私有StickyWindow stickyWindow;



private System.Windows.Forms.CheckBox checkStickToScreen;

private System.Windows.Forms.CheckBox checkStickToOthers;

private System.Windows.Forms.CheckBox checkStickOnResize;

private System.Windows.Forms.CheckBox checkStickOnMove;

///< summary>

// /必需的设计变量。

///

private System.ComponentModel.Container components = null;



public Form2()

{

InitializeComponent();

stickyWindow = new StickyWindow(this);

checkStickOnMove .Checked = stickyWindow.StickOnMove;

checkStickOnResize.Checked = stickyWindow.StickOnResize;

checkStickToOthers.Checked = stickyWindow.StickToOther;

checkStickToScreen.Checked = stickyWindow.StickToScreen;

}



///< summary>

///清理正在使用的任何资源。

///

protected override void Dispose(bool disposing)

{

if (处理)

{

if(components!= null)

{

components.Dispose();

}

}

base.Dispose(disposing);

}



#region Windows窗体设计器生成的代码

///< summary>

/// Designer支持所需的方法 - 不要修改

///使用代码编辑器调用此方法的内容。

///

private void InitializeComponent()

{

this.checkStickToScreen = new System.Windows.Forms.CheckBox();

this.checkStickToOthers = new System.Windows.Forms.CheckBox();

this.checkStickOnResize = new System.Windows.Forms.CheckBox();

this.checkStickOnMove = new System.Windows.Forms.CheckBox();

this.SuspendLayout();

//

// checkStickToScreen

//

this.checkStickToScreen.Location = new System.Drawing.Point(22,26);

this.checkStickToScreen.Name =checkStickToScreen;

this.checkStickToScreen.TabIndex = 0;

this.checkStickToScreen.Text =坚持屏幕;

thi s.checkStickToScreen.CheckedChanged + = new System.EventHandler(this.checkStickToScreen_CheckedChanged);

//

// checkStickToOthers

//

this.checkStickToOthers.Location = new System.Drawing.Point(22,58);

this.checkStickToOthers.Name =checkStickToOthers;

this.checkStickToOthers.TabIndex = 1;

this.checkStickToOthers.Text =坚持其他人;

this.checkStickToOthers.CheckedChanged + = new System.EventHandler(this。 checkStickToOthers_CheckedChanged);

//

// checkStickOnResize

//

this.checkStickOnResize.Location = new System .Drawing.Point(22,122);

this.checkStickOnResize.Name =checkStickOnResize;

this.checkStickOnResize.TabIndex = 2;

this.checkStickOnResize.Text =坚持调整大小;

this.checkStickOnResize.CheckedChanged + = new System.EventHandler(this.checkStickOnR esize_CheckedChanged);

//

// checkStickOnMove

//

this.checkStickOnMove.Location = new System .Drawing.Point(22,154);

this.checkStickOnMove.Name =checkStickOnMove;

this.checkStickOnMove.TabIndex = 3;

this.checkStickOnMove.Text =Stick On Move;

this.checkStickOnMove.CheckedChanged + = new System.EventHandler(this.checkStickOnMove_CheckedChanged);

//

// Form2

//

this.AutoScaleBaseSize = new System.Drawing.Size(5,13);

this.ClientSize = new System.Drawing.Size(292,266);

this.Controls.Add(this.checkStickOnMove);

this.Controls.Add (this.checkStickOnResize);

this.Controls.Add(this.checkStickToOthers);

this.Controls.Add(this.checkStickToScreen);

this.Name =Form2;

this.Text =Form2;

this.Load + = new System。 EventHandler(this.Form2_Load);

this.ResumeLayout(false);



}

#endregion < br $>


private void Form2_Load(object sender,System.EventArgs e)

{

}



private void checkStickToScreen_CheckedChanged(object sender,System.EventArgs e)

{

stickyWindow.StickToScreen = checkStickToScreen.Checked;

}



private void checkStickToOthers_CheckedChanged(object sender,System.EventArgs e)

{

stickyWindow.StickToOther = checkStickToOthers.Checked;

}



private void checkStickOnResize_CheckedChanged(object sender,System.EventArgs e)

{

stickyWindow.StickOnResize = checkStickOnResize.Checked;

}



private void checkStickOnMove_CheckedChanged (对象发送者,System.EventArgs e)

{

stickyW indow.StickOnMove = checkStickOnMove.Checked;

}

}

}

解决方案

< blockquote>这里没有任何谜团:从C#2.0开始,部分类被添加到语言和FrameWork中。你甚至可以使用Partial来扩散结构,接口和方法:虽然,为什么你想要这样做是我永远不会理解的。



部分课程在阳光下并不新鲜; SmallTalk让他们处于石器时代晚期:他们被称为Class Extensions。



他们主要是为开发人员提供方便,并且是一种干净利落的自动生成方式您编写的代码中的代码。



因此,您在WinForms代码中查看程序员代码与Form Control创建,布局和InitializeComponents混合的位置定义等等,只是可能在C#2.0之前编写的旧代码。


I notice in some projects (in Code Project and others), the developers move the "InitializeComponent" and "Dispose" to Form Code (put it in the form)

Why they do so ? and how i can benefit from this move ?


Example :

[][Sticky Windows]

Form 1 :


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //

        //Blue.Windows.StickyWindow.RegisterExternalReferenceForm ( this );
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form 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()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(10, 16);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(102, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "New Test Form";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(350, 53);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            Form2 ff = new Form2();
            ff.Show();
        }
    }
}






Form 2 :



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using Blue.Windows;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form2.
///
public class Form2 : System.Windows.Forms.Form
{
private StickyWindow stickyWindow;

private System.Windows.Forms.CheckBox checkStickToScreen;
private System.Windows.Forms.CheckBox checkStickToOthers;
private System.Windows.Forms.CheckBox checkStickOnResize;
private System.Windows.Forms.CheckBox checkStickOnMove;
/// <summary>
/// Required designer variable.
///
private System.ComponentModel.Container components = null;

public Form2()
{
InitializeComponent();
stickyWindow = new StickyWindow( this );
checkStickOnMove.Checked = stickyWindow.StickOnMove;
checkStickOnResize.Checked = stickyWindow.StickOnResize;
checkStickToOthers.Checked = stickyWindow.StickToOther;
checkStickToScreen.Checked = stickyWindow.StickToScreen;
}

/// <summary>
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.checkStickToScreen = new System.Windows.Forms.CheckBox();
this.checkStickToOthers = new System.Windows.Forms.CheckBox();
this.checkStickOnResize = new System.Windows.Forms.CheckBox();
this.checkStickOnMove = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// checkStickToScreen
//
this.checkStickToScreen.Location = new System.Drawing.Point(22, 26);
this.checkStickToScreen.Name = "checkStickToScreen";
this.checkStickToScreen.TabIndex = 0;
this.checkStickToScreen.Text = "Stick to Screen";
this.checkStickToScreen.CheckedChanged += new System.EventHandler(this.checkStickToScreen_CheckedChanged);
//
// checkStickToOthers
//
this.checkStickToOthers.Location = new System.Drawing.Point(22, 58);
this.checkStickToOthers.Name = "checkStickToOthers";
this.checkStickToOthers.TabIndex = 1;
this.checkStickToOthers.Text = "Stick to Others";
this.checkStickToOthers.CheckedChanged += new System.EventHandler(this.checkStickToOthers_CheckedChanged);
//
// checkStickOnResize
//
this.checkStickOnResize.Location = new System.Drawing.Point(22, 122);
this.checkStickOnResize.Name = "checkStickOnResize";
this.checkStickOnResize.TabIndex = 2;
this.checkStickOnResize.Text = "Stick on Resize";
this.checkStickOnResize.CheckedChanged += new System.EventHandler(this.checkStickOnResize_CheckedChanged);
//
// checkStickOnMove
//
this.checkStickOnMove.Location = new System.Drawing.Point(22, 154);
this.checkStickOnMove.Name = "checkStickOnMove";
this.checkStickOnMove.TabIndex = 3;
this.checkStickOnMove.Text = "Stick On Move";
this.checkStickOnMove.CheckedChanged += new System.EventHandler(this.checkStickOnMove_CheckedChanged);
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.checkStickOnMove);
this.Controls.Add(this.checkStickOnResize);
this.Controls.Add(this.checkStickToOthers);
this.Controls.Add(this.checkStickToScreen);
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
this.ResumeLayout(false);

}
#endregion

private void Form2_Load(object sender, System.EventArgs e)
{
}

private void checkStickToScreen_CheckedChanged(object sender, System.EventArgs e)
{
stickyWindow.StickToScreen = checkStickToScreen.Checked;
}

private void checkStickToOthers_CheckedChanged(object sender, System.EventArgs e)
{
stickyWindow.StickToOther = checkStickToOthers.Checked;
}

private void checkStickOnResize_CheckedChanged(object sender, System.EventArgs e)
{
stickyWindow.StickOnResize = checkStickOnResize.Checked;
}

private void checkStickOnMove_CheckedChanged(object sender, System.EventArgs e)
{
stickyWindow.StickOnMove = checkStickOnMove.Checked;
}
}
}

解决方案

There's no mystery here: beginning with C# 2.0 Partial Classes were added to the language and FrameWork. You can even use Partial to "spread" out Structs, Interfaces, and Methods: although, why you'd want to do that is something I'll never understand.

Partial Classes are nothing new under the sun; SmallTalk had them in the late stone-age: they were called "Class Extensions."

They are primarily a convenience for developers, and a way to cleanly separate automatically generated code from code you write.

So, the WinForms code you look at where the programmer's code is mixed-in with the Form Control creation, and layout, and InitializeComponents is defined, etc., is simply old code probably written before C# 2.0.


这篇关于InitializeComponent和Dispose函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆