增加标签名称



我需要一个增加label name的命令代码。

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

例:

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

我需要在foreach中增加label name (label1, label2, etc.)的数量,我将增加变量ii将在像这样的结构中使用label.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++;
}
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); 
}

假设您有一个标签控件数组,标签,您可以执行以下操作:

int i = 1;
for (int i = 1; i < label.Length; i++)
{
    lbl.ID = String.Format("label{0}", i.ToString());
}

最新更新