using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication2
{
public partial class Form1 : Form
{
private System.Windows.Forms.Label label2;
public Form1() { InitializeComponent();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(80, 114);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
// this.label2.Show();
}
}
}我想动态在Form1上创建一个Label2,可是不行,这是何因呢
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication2
{
public partial class Form1 : Form
{
private System.Windows.Forms.Label label2;
public Form1() { InitializeComponent();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(80, 114);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 12);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
// this.label2.Show();
}
}
}我想动态在Form1上创建一个Label2,可是不行,这是何因呢
this.Controls.Add(label2);
可在这InitializeComponent();里面去声明,就可以实例化啊,我感觉和直接在Form上面添加是一样的
this.Controls.Add(label2);
多谢谢,已解决啦!
{
((Button)sender).Text = "Clicked!";
Button newButton = new Button();
newButton.Text = "New Button!";
newButton.Click += new EventHandler(newButton_Click);
Controls.Add(newButton);
}
这个是动态在Form1上创建一个newButton