Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically changing a winforms button text color doesn't work

Tags:

c#

winforms

When I try to programmatically change a button's text color on my winforms form, it doesn't work. I have this code:

btnTestConnection.Text = "Valid connection";
btnTestConnection.Font = new Font(btnTestConnection.Font, FontStyle.Bold);
btnTestConnection.BackColor = Color.Green;
btnTestConnection.ForeColor = Color.White;
  1. The text changes as expected.
  2. The font changes to bold as expected.
  3. The BackColor changes to green as expected.
  4. The ForeColor remains black - unchanged.

This seems bizarre. I have verified that nothing downstream changes the forecolor back to black. What am I missing?

I am using VS 2017 and compiling my project against .NET Framework 4.5.2.

like image 383
HerrimanCoder Avatar asked Apr 16 '26 19:04

HerrimanCoder


1 Answers

I figured it out. My button is disabled at the time the ForeColor property is being set to white. Apparently winforms won't allow a disabled button to have its ForeColor property changed.

like image 107
HerrimanCoder Avatar answered Apr 18 '26 08:04

HerrimanCoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!