【问题标题】:Looping through labels with similar name C#循环遍历具有相似名称的标签 C#
【发布时间】:2016-07-12 05:07:35
【问题描述】:

我想根据文本文件上的内容更改某些标签的背景:

private void Form3_Load(object sender, EventArgs e)
        {
            string[] words = new string[7];
            StreamReader read = new StreamReader(path);
            while(!read.EndOfStream)
            {
                string line = read.ReadLine();
                words = line.Split(';');
                if(words[6] == "no") 
                {
                    //-----What I have to write here---
                }              
            }
            read.Close();
        }

有超过 50 个标签名为“lbl101”、“lbl102”、“....”、“lbl150”

【问题讨论】:

  • 我认为它不清楚你在问什么。你想在什么条件下贴标签?
  • 把它们全部放在一个堆栈上并改变它的颜色?

标签: c# forms labels


【解决方案1】:

试试看:

if(words[6] == "no") 
{
    int count = 150;
    for (int a = 1 ; a < count; a++)
    {
        Label currentLabel = (Label)this.Controls.Find("lbl"+a,true)[0];    
        //change color of currentLabel
    }
} 

【讨论】:

  • 它不起作用,它无法将类型 System.Windows.Form.Controls[] 转换为 System.Windows.Form.Label
【解决方案2】:

有有效的解决方案:

private void Form3_Load(object sender, EventArgs e)
        {
            int count = 101;
            string[] words = new string[7];
            StreamReader read = new StreamReader(pathRooms);
            while(!read.EndOfStream)
            {
                string line = read.ReadLine();
                words = line.Split(';');
                if (words[6] == "no")
                {

                        Label currentLabel = (Label)this.Controls.Find("lbl" + count, true)[0];
                        currentLabel.BackColor = Color.Yellow;

                }
                count = count + 1;
            }
            read.Close();
        }

【讨论】:

    【解决方案3】:

    您可以在Controls 表单集合上使用OfType&lt;T&gt;() 方法遍历它们,例如:

    if(words[6] == "no") 
    {
        foreach(var label in this.Controls.OfType<Label>().Where(x=>x.Name.Contains("lbl")))
        {
             label.Text = "Some Text";
        }
    }
    

    这仅适用于表单的直接子标签,嵌套在其他用户控件或嵌套面板中的标签不会受到影响,因为you have to do it recursively

    【讨论】:

      【解决方案4】:

      遍历表单的 Controls 集合,检查 Label 对象。然后根据指定的值进行相应的修改。

      【讨论】:

        【解决方案5】:

        1.) 创建一个包含所有标签的列表。

        Label lbl101 = new Label();
        Label lbl102 = new Label();
        ...
        
        List<Label> labels = new List<Label>()
        {
            lbl101,
            lbl102
        ...
        };
        

        2.) 如果你的 words[] 字符串是你可以写的颜色名称:

        if(words[6] == "no") 
        {
           System.Drawing.Color myColor = System.Drawing.ColorTranslator.FromHtml(words[..]);
           foreach(Label l in Labels)
           {
               l.BackColor = myColor;
           }
        }  
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-06-14
          • 1970-01-01
          • 2016-01-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多