Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore C# Windows Forms backcolor

I have a button on a Windows Forms form for which I change the background color to Color.Yellow when it's clicked. When it's clicked again I want to restore it to the original default appearance.

The default backcolor is SystemColor.Control.

When the button is clicked the first time the only thing I change is the

btn.Text = "ABC";
btn.BackColor = Color.Yellow;

When it's clicked again I do

btn.BackColor = SystemColors.Control

The new background does not have the same shading as it originally did before any clicks. The button originally had a background that was not a solid color, but was two slightly different shades of grey. The final color ends up being a solid shade of grey.

I'm testing this on a Windows 7 machine.

Screenshot:

Enter image description here

like image 846
JonF Avatar asked Nov 21 '11 20:11

JonF


4 Answers

Try this:

if (button1.BackColor == Color.Yellow)
{
    button1.BackColor = SystemColors.Control;
    button1.UseVisualStyleBackColor = true;
}
else
{
    button1.BackColor = Color.Yellow;
}
like image 131
mj82 Avatar answered Nov 08 '22 06:11

mj82


You should also set UseVisualStyleBackColor to true. This property gets set to false when you change the backcolor.

like image 35
Arno Kalkman Avatar answered Nov 08 '22 06:11

Arno Kalkman


Try using btn.ResetBackColor() instead of manually setting the BackColor.

like image 24
Marty Avatar answered Nov 08 '22 06:11

Marty


This will restore the default look (tested on Windows 7, .net 3.5):

btn.BackColor = System.Drawing.Color.Transparent; 
like image 24
Nenad Avatar answered Nov 08 '22 07:11

Nenad