Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
If I run this (In Form1_Load, having written no other code in the project):
C#
Color buttonOriginalColor = button1.BackColor;
button1.BackColor = buttonOriginalColor;

the button appears white on the form. If I comment out the second line (or comment out both lines or run it with no code at all written by me in the whole Form application project):
C#
Color buttonOriginalColor = button1.BackColor;
//button1.BackColor = buttonOriginalColor;

now the button comes out the normal gray color, which is slightly darker than I get with the first code. This is also the same color that is shown in the designer.

In both cases, If I put a breakpoint at the end of Form1_Load and look at button1.BackColor , it shows up as Color "{Control,ARGB=(255,240,240,240)}" .

Why does setting the button control BackColor to its own value result in it changing color? Is there something wrong with my installation of Visual Studio 2017 ?

What I have tried:

Nothing. I just want to understand why it is changing the color when I set button1.BackColor to its own value.
Posted
Updated 28-Jun-20 7:55am
v2
Comments
[no name] 28-Jun-20 12:31pm    
Interesting thing, +5 for the question. Btw. I can reproduce this.
[Edit]
When I use colorpicker from e.g. Snagit, the unmodified button shows RGB(225, 225, 225)

Setting the BackColor seems to activate the button, that is why it now looks different.

Try doing it to two buttons, or activate something else afterwards and you'll see...

EDIT

No that is not right, still investigating...

:)
 
Share this answer
 
v3
Comments
[no name] 28-Jun-20 13:04pm    
But if I debug button1 and button2 do show the same 'lighter' color at formload which is not the color with which they are displayed. Confused :)
Luc Pattyn 28-Jun-20 13:16pm    
Indeed, something seems wrong.
The BackColor properties are fine, it is only the way the "modified" buttons are painted that deviates. Also the hover color is way off.
And it misbehaves with or without VisualStyles enabled. ???
And it is the same with good old VS2008...
And also on Win7

One way to get rid of it is to select FlatStyle.System (by default buttons are FlatStyle.Standard) whatever all that might mean.
[no name] 28-Jun-20 13:46pm    
Anyway a 5, this also for the courage to correct it.
I'm trying to find the reason in the reference implementation but it seems to be hopeless :(
Hi,

it is pretty complex after all.

A standard button will behave the way you experienced, no matter what versions of Visual Studio or Windows you are using.

In fact, the button starts right away by not showing the expected color, as its BackColor by default is SystemColors.Control, which is the same for a Form, however it looks a lot darker.

It is only after you explicitly set it to SystemColors.Control that it paints itself using the chosen BackColor.

The culprit is an obscure property UseVisualStyleBackColor which by default is set true. Setting BackColor explicitly does clear this boolean; only then the button will obey its BackColor property.

Another way to get close to intuitive behavior is by setting FlatStyle to FlatStyle.System (by default buttons are FlatStyle.Standard).

I don't understand the rationale of it all, it seems like they keep adding "intelligence" making it all pretty hard to grasp...

:)
 
Share this answer
 
v7
Comments
[no name] 28-Jun-20 13:57pm    
So another 5 :)
AS01234 28-Jun-20 14:13pm    
Thank you all. I discovered the problem when I was simply trying to change the color of a button for a timed period and then change it back to the original color, in order to indicate that a program finished an activity that it was doing in the background. Then I simplified it down to this two-line example. This seems like a very common situation, and it is disconcerting that the Forms controls are behaving in such unexpected ways. I think a work around for my need is to change all of the button colors at the beginning in Form1_Load. What a pain-in-the-neck.
Luc Pattyn 28-Jun-20 14:44pm    
You're welcome.

Performing an operation on all controls of a certain type is doable with a foreach loop on Controls, something like:
foreach (Control c in Controls) {
    Button b=c as Button;
    if (b!=null) b.SomeProperty=SomeValue;
}


If your form is somewhat hierarchical, e.g. some Buttons are located inside a container (such as a Panel) then in general you need recursion to handle that.

:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900