c#中怎样动态生成多个label控件?

.
2024-11-03 16:30:34
推荐回答(3个)
回答1:

private void AddTextBoxs()
{
TableRow tr = new TableRow();
TableCell TCPartNo = new TableCell();
TableCell TCPartName = new TableCell();
TableCell TCQuantity = new TableCell();
TableCell TCOriginalSN = new TableCell();
TableCell TCNewSN = new TableCell();
TextBox TBPartNo = new TextBox();
TextBox TBPartName = new TextBox();
TextBox TBQuantity = new TextBox();
TextBox TBOriginalSN = new TextBox();
TextBox TBNewSN = new TextBox();
TBPartNo.ID = "TBPartNo" + TableNewRow.Rows.Count;
TBPartName.ID = "TBPartName" + TableNewRow.Rows.Count;
TBQuantity.ID = "TBQuantity" + TableNewRow.Rows.Count;
TBOriginalSN.ID = "TBOriginalSN" + TableNewRow.Rows.Count;
TBNewSN.ID = "TBNewSN" + TableNewRow.Rows.Count;
TBPartNo.Width = 119;
TBPartName.Width = 170;
TBQuantity.Width = 70;
TBOriginalSN.Width = 120;
TBNewSN.Width = 120;
TBPartNo.MaxLength = 50;
TBPartName.MaxLength = 50;
TBQuantity.MaxLength = 4;
TBOriginalSN.MaxLength = 50;
TBNewSN.MaxLength = 50;
TCPartNo.Controls.Add(TBPartNo);
TCPartName.Controls.Add(TBPartName);
TCQuantity.Controls.Add(TBQuantity);
TCOriginalSN.Controls.Add(TBOriginalSN);
TCNewSN.Controls.Add(TBNewSN);
tr.Cells.Add(TCPartNo);
tr.Cells.Add(TCPartName);
tr.Cells.Add(TCQuantity);
tr.Cells.Add(TCOriginalSN);
tr.Cells.Add(TCNewSN);
TableNewRow.Rows.Add(tr);
}
通过测试,我做的网站上使用动态添加,我添加的是TextBox

回答2:

代码里

Label lb1=new Label();

这就生成了一个lb1,把lb1这个对象加到父容器上就行了

//本人没有试过此方法,你可以试一下

回答3:

Label lab = new Label();
lab.Text = "&Save";
lab.Size = new Size(100,30);
lab.Location = new Point(0,0);

this.Controls.Add(lab);