When I change a .NET Button's BackColor back to its original value, it doesn't look the same any more
Asked Answered
R

5

10

I have a button which I wanted to "flash" briefly to get the user's attention. I figured the easiest way would be to change the Button's BackColor property to another color, and then switch it back again. So I do something like this:

this.oldColor = myButton.BackColor;
myButton.BackColor = Color.Blue;

and then after a about 1/2 a second:

myButton.BackColor = this.oldColor;

But the button's background color end up being distinctly darker than the rest of the buttons on the Form!

At first, I thought it was because there's something special about the button's original color being a named color (in this case, "Control") but it isn't.

What's worse, when I look at myButton.BackColor in the debugger, I get

{Name=Control, ARGB=(255, 236, 233, 216)}

Which is exactly correct! But when I take a screenshot, and check the color, it isn't the same as the rest of the buttons!

Is there some kind of masking going on? Or perhaps some kind of dithering?

Roberson answered 3/2, 2009 at 10:28 Comment(0)
R
14

I figured out the cause for the problem. Turns out there's another property (for Buttons and TabPages only, it seems) named UseVisualStyleBackColor. It controls whether or not to use "Visual Styles" when calculating the BackColor. And to make matters worse, as soon as you set the BackColor, it's set to false. So I just ended up doing this:

this.oldUseVisualStyleBackColor = myButton.UseVisualStyleBackColor;
this.oldColor = myButton.BackColor;
myButton.BackColor = Color.Blue;

And when I'm ready to reset it:

myButton.BackColor = this.oldColor;
myButton.UseVisualStyleBackColor = this.oldUseVisualStyleBackColor;

(Yes, you have to reset the BackColor first, and only then set UseVisualStyleBackColor.)

I have no idea why the UseVisualStyleBackColor is being used, or why it does what it does, but it seems to work now.

(Thanks, Marc! I wouldn't have found this without your help!)

Roberson answered 3/2, 2009 at 11:24 Comment(1)
@scraimer: +1. Thanks for documenting this problem. I had a similar problem and found your post via Google.Thalassa
C
3

I suspect the difference is that one is a regular argb color, and one is a system/known color.

Controls in .NET keep track of whether the color is explicit (set on this control) or inherited. This makes it hard to change back properly... but you might be able to do it with PropertyDescriptor, like so:

    TextBox tb = new TextBox();
    tb.BackColor = Color.Red;
    // now set it back to inherited
    PropertyDescriptor prop = TypeDescriptor.GetProperties(tb)["BackColor"];
    if (prop.CanResetValue(tb))
    {
        prop.ResetValue(tb);
    }

A bit clunky, but it should work.

Clarkclarke answered 3/2, 2009 at 10:30 Comment(0)
G
3

The UseVisualStyleBackColor property of button determines whether the background of the button is drawn using visual styles. When the UseVisualStyleBackColor property of a button is set to True, the BackColor property is ignored. When the BackColor property is changed to a different color, the UseVisualStyleBackColor property is automatically set to False.

You can see this effect when you are in design mode. Create a button and change the BackColor property to red. Then scroll down to the UseVisualStyleBackColor property, and note that it has been set to False. If you then set the UseVisualStyleBackColor property to True, the button color will change back to the default color. Switching the UseVisualStyleBackColor property back to False will change the button color back to red.

The following program demonstrates this effect.

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Button1.BackColor = Color.Red
End Sub

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Button1.UseVisualStyleBackColor = Not Button1.UseVisualStyleBackColor
End Sub

Thanks to scraimer for posting his problem and identifying the UseVisualStyleBackColor property as the guilty culpert.

Ganger answered 12/4, 2013 at 17:6 Comment(0)
M
2

Or you could just type:

TextBox tb = new TextBox();
//Change the Backcolor
tb.BackColor = Color.Red;
//Resets the Backcolor to its default value, its pretty strange that you don't see the method but it works with allmost all properties
tb.ResetBackColor();
Marijn answered 9/3, 2010 at 10:24 Comment(1)
Cool! Well, you learn something new every day. I haven't tried it, but I'll try it next time I need this. Thanks!Roberson
R
0

Simple way:

myButton.BackColor = Color.Empty
Recuperative answered 1/3, 2012 at 22:51 Comment(1)
This doesn't work for me in .NET 2.0 - can anyone else confirm it works?Roberson

© 2022 - 2024 — McMap. All rights reserved.