【问题标题】:Looping through labels in C#在 C# 中循环遍历标签
【发布时间】:2016-03-24 13:04:31
【问题描述】:

我有九个标签,名称分别为"lbl101""lbl102"、...

我想这样做:

for (int i = 0; i < 9; i++)
{
    sting name = "lbl10" + i;
    name.BackColor = Color.Red;
}

我该怎么做?

【问题讨论】:

  • 字符串没有 BackColor 属性。
  • 请确认这是 Windows 窗体、WPF 还是 HTML 窗体。

标签: c# forms winforms label


【解决方案1】:

您可以将控件添加到集合中,然后循环遍历它。

var labels = new List<Label> { lbl101, lbl102, lbl103 };

foreach (var label in labels)  
{  
    label.BackColor = Color.Red;  
}

或者,如果您只希望Form 上以“lbl10”开头的每个Label,您可以使用 LINQ 查询控件集合:

var labels = this.Controls.OfType<Label>()
                          .Where(c => c.Name.StartsWith("lbl10"))
                          .ToList();

【讨论】:

  • 但是如果我有 90 或 900 个标签,我该怎么办?
【解决方案2】:

如果表单上设置了标签,可以使用Linq

  var labels = Controls // or MyPanel.Controls etc. if labels are on panel
    .OfType<Label>()
    .Where(label => label.Name.StartsWith("lbl10"));

  foreach (var label in labels)   
    label.BackColor = Color.Red;  

【讨论】:

  • 应该是label.Name.StartsWith,你有错字。
  • @juunas:谢谢! With 不是 Width,这是肯定的 :)
【解决方案3】:

遍历它们所在的容器并获取对它们的引用。

for(int i = 0; i<9; i++)  
{  
   var label = (Label)yourForm.FindControl("lbl10" + i.ToString());  
   label.BackColor = Color.Red;  
}

【讨论】:

  • 你的意思是yourForm.Controls["lbl10" + i]
  • OP 在哪里提到 windows 窗体?
  • @StephenBrickner 他的标签有一个.BackColor 属性。这有点赠品。
  • 也许是这样,但这不是 FindControl() 返回的内容。但我想我们需要 OP 来确认一种或另一种方式。
  • 在 FindControl 上说:“myForm”不包含定义“FindControl”
【解决方案4】:

可能最简单的方法是将它们全部列出:

lbl100.BackColor = Color.Red;
lbl101.BackColor = Color.Red;
lbl102.BackColor = Color.Red;
lbl103.BackColor = Color.Red;
lbl104.BackColor = Color.Red;
lbl105.BackColor = Color.Red;
lbl106.BackColor = Color.Red;
lbl107.BackColor = Color.Red;
lbl108.BackColor = Color.Red;

这是最直接的方法。如果您真的想花哨,可以将它们全部放在一个数组中并对其进行迭代:

Label[] labels = new Label[]{
    lbl100, lbl101, lbl102, lbl103, lbl104, lbl105, lbl106, lbl107, lbl108
};
for (int i = 0; i < labels.Length; i++)
{
    labels[i].BackColor = Color.Red;
}

或者,如果您知道所有标签都是某个控件的子级,并且该控件中没有其他标签,您可以这样做:

foreach (Control c in someControl.Controls)
{
    if (c is Label)
    {
        ((Label)c).BackColor = Color.Red;
    }
}

【讨论】:

    【解决方案5】:
     public void changebackground()
     {           
         Label mylabel;
    
         foreach (Control con in this.Controls) 
         {
             if (con.GetType() == typeof (Label)) //or any other logic
             {
                 mylabel = (Label)con;
                 mylabel.BackColor = Color.Red;
             }
         }
     }
    

    【讨论】:

      【解决方案6】:

      在Windows窗体中,要指向一个控件,只需用下面的语句调用它

      this.Controls[control name]
      

      例如

      this.Controls["label1"].BackColor = Color.Blue;
      

      所以你的问题的答案

      for (int i = 0; i < 9; i++)
      {  
        this.Controls["lbl10" + i.ToString()].BackColor = Color.Red;
      }
      

      【讨论】:

        猜你喜欢
        • 2018-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多