1

我需要一个增加label name.

我需要在 10 个(示例)中显示labels一些文本。

例子:

label1.Text = "1";
label2.Text = "2";
label3.Text = "3";
label4.Text = "4";
label5.Text = "5";
label6.Text = "6";

我需要从增加变量label name (label1, label2, etc.)的地方增加数字(将在这样的结构中使用)。foreachiilabel.Name = "label" + i.ToString();

我希望你明白我想说什么。

我试试这个但不工作:

Label[] label = new Label[2];

int ii = 0;
foreach(...) // go through a list
{
                label[ii] = new Label();
                label[ii].Text = x.materie + tip + "\nsala " + x.sala;
                label[ii].Visible = true;
                label[ii].Location = new System.Drawing.Point(cX, cY);
                label[ii].SetBounds(cX, cY, 98, cH);
                label[ii].MinimumSize = new Size(98, cH);
                label[ii].MaximumSize = new Size(98, cH);

                ii++;
}
4

2 回答 2

3
int count = 10;
for (int i = 1; i <= count; i++)
{
    // setup label and add them to the page hierarchy
    Label lbl = new Label();
    lbl.Name = "label" + i;
    lbl.Text = i.ToString();
    //assuming form1 is a form in your page with a runat="server" attribute.
    this.Controls.Add(lbl); 
}
于 2012-10-07T10:59:57.103 回答
0

假设你有一个 Label 控件数组,label,你可以执行以下操作:

int i = 1;

for (int i = 1; i < label.Length; i++)
{
    lbl.ID = String.Format("label{0}", i.ToString());
}
于 2012-10-07T11:15:40.867 回答