0

I have 30 buttons on my form, and I want to add them to a list and then change the forecolor of them all using foreach

List<Button> buttons = new List<Button> { button3, button4, button5......etc };
foreach (Button btn in buttons)
{
    btn.ForeColor = Color.White;
}

However the color of the buttons never actually changes.

Carlos Muñoz
  • 17,397
  • 7
  • 55
  • 80
christian_chr
  • 77
  • 2
  • 11
  • 8
    Please be more specific about the "does not work" part: does it "crash", "does not compile", or "has no visible effect"? – Sergey Kalinichenko Dec 23 '15 at 15:28
  • Possible duplicate of [Generic All Controls Method](http://stackoverflow.com/questions/17454389/generic-all-controls-method) – Sayse Dec 23 '15 at 15:28
  • @dasblinkenlight Nothing happens, i thought the code is incomplete or incorrect – christian_chr Dec 23 '15 at 15:29
  • Is the code even reached? Do you run it with the debugger and step through the code? – mason Dec 23 '15 at 15:30
  • 2
    but where did you put this code? what is this, winform? – sowen Dec 23 '15 at 15:30
  • It could be [Winforms](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.forecolor.aspx) or [Webforms](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.forecolor.aspx) – Carlos Muñoz Dec 23 '15 at 15:33
  • It is Winforms, i put this code under a button click event – christian_chr Dec 23 '15 at 15:33
  • So you put the code under an event, but did you ever verify that the code is *actually reached*? Do you know how to use the debugger, or add console or trace output statements? – mason Dec 23 '15 at 15:35
  • Are you sure that your list contains actual buttons that are placed on your form? – msmolcic Dec 23 '15 at 15:38
  • Make sure the FlatStyle property of the buttons are set to Standard. – LarsTech Dec 23 '15 at 15:41

1 Answers1

0

In my opinion the problem is on the ForeColor property. I would use the BackColor/Background property to change the color.

If you are using WinForms :

button.BackColor = Color.White;

If you are using WPF :

button.Background = Brushes.White;
Snak
  • 673
  • 1
  • 5
  • 19
  • 1
    Perhaps it would help if you described the difference between ForeColor and BackColor instead of just saying to use the other one. – mason Dec 23 '15 at 15:36
  • It seems like you were right, what i actually wanted was to change the BackColor, now i know the difference between them two – christian_chr Dec 23 '15 at 15:39
  • @mason well I don't really know what changing the ForeColor property does but I know what the BackColor does. – Snak Dec 23 '15 at 15:51
  • I have little experience with WinForms, but typically ForeColor is the color of the text, and BackColor is the color of the rest of the button (the space around the text within the borders of the button). – mason Dec 23 '15 at 15:54
  • ForeColor applies to the content of a control. So for instance the text color – srandppl Dec 23 '15 at 15:54