[英]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.