繁体   English   中英

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

[英]c# How to change the visible property of a label created at runtime

//这里我一键式在运行时创建标签

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;
}

//在一个计时器刻度的私有空隙中,我将标签名称分配给变量“ a”,然后执行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;
    }
}

//第三种方法

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

不幸的是,没有一个有效。

有人知道发生了什么事吗?

您没有将标签添加到拥有控件。 因此它们将永远不会显示。 因此,在循环中,您需要添加以下内容作为最后一行...

this.Controls.Add(labels[i]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM