1

I have a fairly simple question. I want to change the background color of 15 buttons, but is is very cumbersome to write

button1.backgroundColor = Color.black
button2.backgroundColor = Color.black
.
.
.

How i can assign the color to all these buttons at once?

Like:

something allButtons = { button1.backgroundColor, button2.backgroundcolor .... }

I know how to do this in obj-c:

for (int i = 1; i < 16; i++)
{
    UIButton *button = (UIButton *)[tag:i];
    button.backgroundColor = [UIColor Color.black];
}

but how to accomplish this in C#?

alix54
  • 444
  • 1
  • 5
  • 13

2 Answers2

4

You might be able to iterate through all the controls on the form, looking for buttons.

    foreach (Control c in this.Controls)
    {
        if (c.GetType() == typeof(Button))
        {
            c.BackColor = Color.Black;
        }
    }
John Go-Soco
  • 886
  • 1
  • 9
  • 20
  • 2
    I think, `c is Button` is better condition. – Vlad Dec 23 '14 at 08:53
  • 3
    @Vlad: and even better - `Controls.OfType – Dennis Dec 23 '14 at 09:07
  • 2
    Not if the buttons are inside some container and not in the main Form.Controls container. Looping in this way will change only the buttons that are in the root container. (IE. Try to place a button inside a GroupBox. – Steve Dec 23 '14 at 09:26
  • Yep, you would have to do a recursive loop if you've got containers. Here's an even better implementation: http://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-windows-forms-form-of-a-specific-type-button – John Go-Soco Dec 23 '14 at 10:22
1

You put your buttons in an button array

Button[] buttons = new Button[] {button1, button2, .....};

Or in a List<Button>

List<Button> buttons = new List<Button>() { button1, button2, ....);

Next you loop over the button array or the List in the same way

foreach(Button btn in buttons)
    btn.BackColor = Color.Black;

Another way to change this property is looping using the Forms.Controls container. But this will work only if the buttons are all contained in the Form.Controls collection.

foreach (Control btn in this.Controls.OfType<Button>())
{
    btn.BackColor = Color.Black;
}

To fix the problem of buttons contained in inner ControlCollection you should use a recursive function that loops on every control container and reach buttons eventually inside that container

public void SetBackground(Control.ControlCollection coll)
{
    foreach(Control ctr in coll)
    {
       if(ctr.Controls.Count > 0)
          SetBackground(ctr.Controls);
       else
       {
          Button btn = ctr as Button;
          if(btn != null) btn.BackColor = Color.Black;
       }
    }
}

and call it from the toplevel collection

SetBackground(this.Controls);

It is a lot more complicated, so I prefer to use an array to explicitily declare the buttons that need to be changed.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • This assumes, that someone must create this array. While this is obvious and universal solution, it is rather ugly, because, depending on UI framework, controls are contained already somewhere. OP just need to determine the scope, in which buttons should be affected. – Dennis Dec 23 '14 at 09:09
  • Perhaps you are right, but creating the array will give a better control on which buttons should be changed in the loop. And we are talking about WinForms. – Steve Dec 23 '14 at 09:24
  • Better use a List – TaW Dec 23 '14 at 09:36
  • @TaW yes this would be better, but probably in this context the list is of no practical advantage because I suppose that the buttons are all know before – Steve Dec 23 '14 at 09:40