简体   繁体   中英

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? 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. Naming helps, like FooBarLabel matches FooBarTextBox . Now you can simply use the Controls collection to find the label back:

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

But Winforms solves many problems by simple inheritance. 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. Boomshakalaka.

You can first manually set your TextBox 's Tag property to these labels. Tag is meant to contain user-defined data, so you can place any object there. 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)
  3. Creating a Dictionary<TextBox, Label> or Dictionary<Control, Label> , which allows resolving such matters in runtime (variation on Steve's idea).

I suppose that you are using 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 ???
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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