简体   繁体   English

如何遍历所有textBox控件并更改关联的标签颜色

[英]How to loop through all textBox controls and change associated label color

        foreach (Control c in this.Controls)
        {
           if (c is TextBox && c.Text.Length==0)
           {
               // [Associatedlabel].ForeColor = System.Drawing.Color.Red;
               err = true;
           }

instead of [Associatedlabel], I want to associate each textbox to label, so eventually all labels near the textbox that are empty will be red, how it can be done? 我想将每个文本框与标签相关联,而不是[Associatedlabel],所以最终文本框附近的所有空白标签都是红色的,怎么办? Thanks. 谢谢。

There is no fantastic way to find the label control back from the textbox. 没有找到从文本框返回标签控件的绝佳方法。 Using the form's GetChildAtPoint() method is something you can make easily work but are going to regret some day. 您可以轻松使用表单的GetChildAtPoint()方法,但是有一天会后悔的。 Naming helps, like FooBarLabel matches FooBarTextBox . 命名有帮助,​​就像FooBarLabel匹配FooBarTextBox Now you can simply use the Controls collection to find the label back: 现在,您可以简单地使用Controls集合来查找标签:

 var label = (Label)this.Controls[box.Name.Replace("TextBox", "Label")];

But Winforms solves many problems by simple inheritance. 但是Winforms通过简单的继承解决了许多问题。 Add a new class to your project and paste this code: 在您的项目中添加一个新类,并粘贴以下代码:

using System;
using System.Windows.Forms;

class LabeledTextBox : TextBox {
    public Label Label { get; set; }
}

Compile and drop the new control from the top of the toolbox. 编译并从工具箱顶部放下新控件。 Set the Label property in the designer, just pick it from the dropdown list. 在设计器中设置Label属性,只需从下拉列表中选择即可。 Boomshakalaka. Boomshakalaka。

You can first manually set your TextBox 's Tag property to these labels. 您可以首先手动将TextBox的Tag属性设置为这些标签。 Tag is meant to contain user-defined data, so you can place any object there. 标签旨在包含用户定义的数据,因此您可以在其中放置任何object Then you can do simply: 然后,您可以简单地执行以下操作:

foreach (Control c in this.Controls)
{
    if (c is TextBox && c.Text.Length==0 && c.Tag is Label)
    {
        ((Label)c.Tag).ForeColor = System.Drawing.Color.Red;
        err = true;
    }
}

This is the simplest solution, but a few more sophisticated exists though. 这是最简单的解决方案,但是还存在一些更复杂的解决方案。

  1. Creating a custom composite control consisting of a label, textbox and custom behavior; 创建一个由标签,文本框和自定义行为组成的自定义复合控件;
  2. Creating a control deriving from a textbox, which stores information about label it is connected with (as Hans Passant suggests) 创建一个从文本框派生的控件,该控件存储有关与其连接的标签的信息(如Hans Passant所建议的)
  3. Creating a Dictionary<TextBox, Label> or Dictionary<Control, Label> , which allows resolving such matters in runtime (variation on Steve's idea). 创建一个Dictionary<TextBox, Label>Dictionary<Control, Label> ,从而可以在运行时解决此类问题(Steve的想法有所不同)。

I suppose that you are using WinForms. 我想您正在使用WinForms。 In this environment you don't have any built-in functionality that associate a label to a textbox. 在这种环境下,您没有任何将标签关联到文本框的内置功能。 So you need to build your own association. 因此,您需要建立自己的关联。

This could be done creating a dictionary in the constructor of your code 这可以在代码的构造函数中创建字典来完成

public class MyForm : Form
{
     private Dictionary<string, Label> assoc = new Dictionary<string, Label>();
     public MyForm()
     {
         // Key=Name of the TextBox, Value=Label associated with that textbox
         assoc.Add("textbox1", Label1);
         assoc.Add("textbox2", Label2);
         assoc.Add("textbox3", Label3);
     }
}

.....
foreach (TextBox t in this.Controls.OfType<TextBox>())
{
   if(t.Text.Length == 0)
   {
        assoc[t.Name].ForeColor = System.Drawing.Color.Red;
        err = true;
   }
   else
        assoc[t.Name].ForeColor = ??? system forecolor ???
}

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

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