Initialize Component方法究竟在做什么?

时间:2013-07-29 06:07:19

标签: c# initialization

在我的项目InitializeComponent()中;方法多次使用。我的问题是,Initialize Component方法究竟在做什么?

1 个答案:

答案 0 :(得分:1)

如果你在VS中查看你的文件,你可以看到一点" +"在他们旁边签名并打开那个和设计师。在那里,您会找到InitializeComponent方法来启动您在表单中添加的控件

InitializeComponent由VS根据您在表单中设置的控件生成

例如,如果我的表单如下所示:

enter image description here

然后InitializeComponent将是:

  private void InitializeComponent()
  {
     this.button1 = new System.Windows.Forms.Button();
     this.SuspendLayout();
     // 
     // button1
     // 
     this.button1.Location = new System.Drawing.Point(85, 36);
     this.button1.Name = "button1";
     this.button1.Size = new System.Drawing.Size(75, 23);
     this.button1.TabIndex = 0;
     this.button1.Text = "MY BTN";
     this.button1.UseVisualStyleBackColor = true;
     // 
     // Form1
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
     this.ClientSize = new System.Drawing.Size(284, 262);
     this.Controls.Add(this.button1);
     this.Name = "Form1";
     this.Text = "Form1";
     this.ResumeLayout(false);

  }