c#如何更改在运行时创建的标签的visible属性

时间:2015-03-30 00:08:56

标签: c# label visible invisible

//在这里,我只需点击一下即可在运行时创建标签

Label[] labels = new Label[countresult];

for (int i = 1; i < countresult; i++)
{
    labels[i] = new Label();
    labels[i].Font = new Font("Arial Rounded MT Bold", 30);
    labels[i].ForeColor = System.Drawing.Color.Red;
    labels[i].AutoSize = true;
    labels[i].Text = "";

    //Here I try to assign the value visible = true

    labels[i].Visible = true;
    labels[i].TabIndex = i;
}

//在计时器滴答声的私有空隙中,我将标签的名称分配给var&#34; a&#34;我做了3种方法

string a = string.Format("labels[{0}]", labelscount);

//第一种方法

if (this.Controls.ContainsKey(a))
{
    this.Controls[a].Visible=false;
}

//第二种方法

foreach (Control control in Controls)
{
    if (control.Name == a)
    {
        control.Visible = false;
    }
}

//第3种方法

if (this.Controls[a] is Label) this.Controls[a].Visible=false;
labelscount++;

不幸的是没有效果。

有人知道发生了什么事?

1 个答案:

答案 0 :(得分:1)

您没有将标签添加到拥有控件。所以他们永远不会被展示出来。所以在你的循环中你需要添加以下作为最后一行......

this.Controls.Add(labels[i]);
相关问题