通过对Lable的Name赋值进行了动态的创建,代码贴下: public void initialzition()
{
for (int i = 1; i <= 4; i++)
{
Label l1 = new Label();
l1.Name = "Lable" + i;
richTextBox1.Text += l1.Name;
l1.Location = new Point(i*10+10,90);
l1.BorderStyle = BorderStyle.Fixed3D;
l1.Width = 10;
l1.Height = 10;
l1.BackColor = Color.Green;
ar.Add(new Point(i*10+10, 90));
panel1.Controls.Add(l1);
//this.Controls.Add(l1);
//l1.Dispose();
}
}
那么 如果我想调用这4个动态创建的"Lable"该怎么做?
比如我要把第四个"Lable4"的width属性改变。
{
for (int i = 1; i <= 4; i++)
{
Label l1 = new Label();
l1.Name = "Lable" + i;
richTextBox1.Text += l1.Name;
l1.Location = new Point(i*10+10,90);
l1.BorderStyle = BorderStyle.Fixed3D;
l1.Width = 10;
l1.Height = 10;
l1.BackColor = Color.Green;
ar.Add(new Point(i*10+10, 90));
panel1.Controls.Add(l1);
//this.Controls.Add(l1);
//l1.Dispose();
}
}
那么 如果我想调用这4个动态创建的"Lable"该怎么做?
比如我要把第四个"Lable4"的width属性改变。
都被同一个名字覆盖了。
没有名字很麻烦,只能根据其在Controls集合中的顺序判断。
(this.Controls["label4"] as Label).Width = xxx;
试试
不行啊 提示没有实例 我把l1.Name = "Lable" + i;改为l1.Name = "Lable" + i.Tostring();
也不行啊
代码能借看看么?或者其他替代方法也行
{
for (int j = 1; j <= 13; j++)
{
int num = 75 * j;
int num1 = 40 * i;
Label lable = new Label();
lable.Location = new System.Drawing.Point(num, num1);
lable.Name = cinema.SeatDy[index].SeatNum;
lable.Size = new System.Drawing.Size(85, 23);
lable.TabIndex = 0;
lable.Text = cinema.SeatDy[index].SeatNum;
lable.ForeColor = System.Drawing.Color.White;
lable.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); ;
lable.BackColor = cinema.SeatDy[index].Color;
lable.Size = new System.Drawing.Size(48, 22);
lable.Click += new System.EventHandler(label1_Click);
lb.Add(lable);
index++;
private Label[] lbs = new Label[4]; public void initialzition()
{
for (int i = 1; i <= 4; i++)
{
Label l1 = new Label();
lbs[i] = l1;
l1.Name = "Lable" + i;
richTextBox1.Text += l1.Name;
l1.Location = new Point(i*10+10,90);
l1.BorderStyle = BorderStyle.Fixed3D;
l1.Width = 10;
l1.Height = 10;
l1.BackColor = Color.Green;
ar.Add(new Point(i*10+10, 90));
panel1.Controls.Add(l1);
//this.Controls.Add(l1);
//l1.Dispose();
}
}