question

PeterHibbs-9836 avatar image
0 Votes"
PeterHibbs-9836 asked PeterHibbs-9836 commented

How can I change the fore color of a button

I have this code (from another Web site) which is supposed to change the button ForeColor from black to gray when the button is disabled but nothing happens, is there any way to fix it.

 Private Sub cmdShowTarget_EnabledChanged(sender As Object, e As EventArgs) Handles cmdShowTarget.EnabledChanged
         If cmdShowTarget.Enabled = False Then cmdShowTarget.ForeColor = Color.Gray Else cmdShowTarget.ForeColor = Color.Black
 End Sub

If I use the BackColor property like this - then it works correctly!!

 Private Sub cmdShowTarget_EnabledChanged(sender As Object, e As EventArgs) Handles cmdShowTarget.EnabledChanged
       If cmdShowTarget.Enabled = False Then cmdShowTarget.BackColor = Color.Gray Else cmdShowTarget.BackColor = Color.Black
 End Sub

Peter Hibbs.

windows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered PeterHibbs-9836 commented

You can see this old thread : Changing Disabled Controls ForeColor
and answer from DMan1


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,

Thanks, I did come across that post when I was researching this problem but I didn't understand it and it looked far too complicated for this simple problem and my level of VB.NET knowledge at the moment.

So are you saying that there is a bug in Visual Studio that it is not possible to change the text color of a button - which seems very odd to me! If so, then I think I will just leave things as they are (or maybe change the back-color to indicate that the button is disabled).

Thanks anyway.

Peter Hibbs.

0 Votes 0 ·

So are you saying that there is a bug in Visual Studio that it is not possible to change the text color of a button - which seems very odd to me!

You can change the ForeColor when it is enabled
But the disabled state is by default handled by Windows
In C++, the main method is more complicated, by using an OwnerDraw Button


0 Votes 0 ·

OK, so does that mean that there is no way to set the color of the text when the button is disabled. I have noticed that even if I set the ForeColor property to 'Gray' and the Enabled property to False in Design mode, when the program runs the text color reverts to black which is annoying to say the least (and NOT something that happens in MS Access).

If so, then I guess I will have to live with that (until Microsoft come to their senses but I am not holding my breath).

Thanks anyway for the extra information.

Peter Hibbs.

0 Votes 0 ·