views:

52

answers:

2

Is there an easy way to set the background color of all objects on a form? Im trying to do it through click event when everything is running. So there would be more then one button. What I would like to avoid is having:

changeColor_Click
{
  label1.BackColor = Color.Black;
  label2.BackColor = Color.Black;
  label3.BackColor = Color.Black;
  etc...
}

What I am looking for:

changeColor_Click
{
 all.BackColor = Color.Black;
}

Keep in mind that each label is a different color backgrounds to start on the GUI:

label1 = blue

label2 = red

label3 = yellow

I have a lot of different objects and am trying to find a good way to switch between themes. Any suggestions on how I could achieve this?

+4  A: 

You have to use Recursion.

Pardon my lousy c#, have not used it in years, you get the idea...

ChangeColor_Click
{
   ChangeAllBG(this);
}

void ChangeAllBG(Control c)
{
    c.BackColor=Color.Teal;
    foreach (Control ctl in c.Controls)
        ChangeAllBG(ctl);
}
FastAl
Thank you, this worked perfectly. Makes a lot of sense too.
Scott's Oasys
Wow did I get lucky if that worked! remember to accept this as the answer. That give me more reputation points. Which I can offer as bounty. Which I can use to get other people to do my job for me for free! Much appreciated.
FastAl
A: 
void SetBackColorRecursive(Control control, Color color)
{
    control.BackColor = color;

    foreach (Color c in control.Controls)
        SetBackColorRecursive(c, color);
}

Call this method on your form like this: SetBackColorRecursive(this, Color.Black);

BlueCode