1

I have nine labels with the names "lbl101", "lbl102", ...

I want to do this:

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

How can I do this?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
adrianoBP
  • 187
  • 6
  • 19

6 Answers6

9

You can add the controls to a collection, and loop through that.

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

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

Alternatively, if you just want every Label on the Form that starts with "lbl10", you can use LINQ to query the collection of controls:

var labels = this.Controls.OfType<Label>()
                          .Where(c => c.Name.StartsWith("lbl10"))
                          .ToList();
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
4

If labels are set on the form, you can use 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;  
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

Loop through the container they are in and grab a reference to them.

for(int i = 0; i<9; i++)  
{  
   var label = (Label)yourForm.FindControl("lbl10" + i.ToString());  
   label.BackColor = Color.Red;  
}
Stephen Brickner
  • 2,584
  • 1
  • 11
  • 19
0

Probably the simplest thing would be to list them all out:

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;

This is the most straightforward way to do it. If you really want to be fancy, you could put them all in an array and iterate over that:

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;
}

Or, if you know that all the labels are children of a certain control and there are no other labels in that control, you could do this:

foreach (Control c in someControl.Controls)
{
    if (c is Label)
    {
        ((Label)c).BackColor = Color.Red;
    }
}
adv12
  • 8,443
  • 2
  • 24
  • 48
0
 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;
         }
     }
 }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Haim Katz
  • 455
  • 3
  • 15
0

In Windows Form, to point to a control, just call it with the following sentence

this.Controls[control name]

for example

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

So the answer to your question

for (int i = 0; i < 9; i++)
{  
  this.Controls["lbl10" + i.ToString()].BackColor = Color.Red;
}
Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33