How to reset to default button BackColor?
Asked Answered
C

11

50

I was experimenting with different options for the button background color and ended up changing the BackColor property in the Appearance category. I then changed it back to what it is by default which is Control, but it still looks different from other buttons:

Screenshot of buttons

I've tried building the project and restarting Visual Studio, but it did not help.

I know I can just drag another button and copy/paste code, but what causes this in the first place and how do I properly fix it?

Corot answered 13/5, 2012 at 3:50 Comment(1)
If changed in design mode then just go to [back color] property (in design mode) and right click. You will get a menu to revert back to default.Infection
F
86

The BackColor property is an ambient property by default, meaning that it inherits its value from its parent control. When you set it explicitly to a particular value, that overrides the ambient nature and forces it to use that particular value.

The standard Windows button control does not support custom colors, so WinForms will actually custom draw the control to allow the designer to override its color. That way, if you want to have an ugly green or red button, you can do that.

What's happened here is you've effectively set a custom background color for the button control (you set it to the background color of a 3D control, but it could just as easily have been purple), and that's forced WinForms to custom draw the control and paint its background with your specified color. That's what gives it the "flat" appearance—the background color is now painted with a single custom color, rather than using the default gradient effect. It wouldn't have been as noticeable under the Windows Classic (pre-Aero) theme, because buttons actually were painted with the flat 3D control color. But Aero added gradients and other "hot" effects, which makes this stick out like a sore thumb.

To clear the value you've set it to and restore the ambient nature of the property, you can right-click on the property in the Properties Window, and select "Reset". You can also do it through code by setting the property to default(Color):

myButton.BackColor = default(Color);

You will also need to set the UseVisualStyleBackColor property back to true, which gets automatically set to false whenever the BackColor property is changed to support the custom background color.

Alternatively, you can tell WinForms to ignore custom properties like that altogether and ask Windows to draw the button control. Do this by setting the FlatStyle property to FlatStyle.System.

Again, this can be done either in the designer or through code. Not only will this prevent you from altering silly things like the background color, creating a horridly ugly control, but it will also restore the native behavior of the Win32 button control to your WinForms application, including the subtle Aero fade effects on hover.

I have no idea why this was not the default. You should have to make a special request to get ugly and non-standard controls. That shouldn't just happen automatically. I can only assume it was a concession to VB 6 programmers, who have been able to make all sorts of ugly controls for years.

Flavescent answered 13/5, 2012 at 4:3 Comment(2)
What is the corresponding VB.NET version of default(Color)?Greiner
Nowadays, you can also call the method button1.ResetBackColor(), but you still need to set button1.UseVisualStyleBackColor = true.Interphone
S
14

When you change the default back color, it flips the UseVisualStyleBackColor property to false.

Just change that back to true and you should be set!

Subheading answered 13/5, 2012 at 4:9 Comment(0)
G
14

To restore the default background color of the button, use;

Button1.BackColor = SystemColors.ButtonFace
Button1.UseVisualStyleBackColor = True
Gentilism answered 21/8, 2014 at 6:0 Comment(2)
UseVisualStyleBackColor = true was enough for me to make it work.Lavish
This one worked for me too. I agree with @Sebastian Ax it was enough to use just UseVisualStyleBackColor = true;Latreese
P
5

To restore the default background and foreground to a button use:

Button1.BackColor = SystemColors.ButtonFace;
Button1.ForeColor = default(Color);
Button1.UseVisualStyleBackColor = true;
Palliasse answered 18/2, 2018 at 19:23 Comment(3)
'SystemColors' does not contain a definition for 'ButtonFace'Refute
are you using Notepad for programming ?Palliasse
Real progrmmers use vi :-)Refute
K
4

What worked for me:

CommandButton1.BackColor = vbButtonFace
Kao answered 1/5, 2015 at 13:43 Comment(0)
M
3
Button1.BackColor = Color.FromKnownColor(KnownColor.Control)

Use the FromKnowColor to access the system colors. It is really that simple.

Mansfield answered 28/10, 2016 at 17:58 Comment(0)
T
2
Button1.BackColor = Color.Gainsboro
Transgression answered 22/5, 2018 at 21:42 Comment(2)
Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.Trochanter
Great, this comment helpful for reset default color's buttonRoveover
Z
1

In Winforms the default color is Control. You can set it from the Properties Pane in VS.

PropertyName is BackColor and the color you want is in System Colors -> Control

Zeralda answered 25/4, 2018 at 11:46 Comment(0)
L
1

**Use this please **

myButton.BackColor = default(Color);
Landscapist answered 3/4, 2019 at 12:49 Comment(0)
E
0

Referring to your question title, "How to reset to default button BackColor?" , I solved my problem by:

Create another button, say ButtonFoo, and set it to invisible,

ButtonFoo.visible = False

and you can use this button to get its color (since it is a default color) to reset your another button color, for example,

ButtonChangedColor.BackColor = ButtonFoo.BackColor

There you go; color is restored to its default :)

Euchromosome answered 20/5, 2014 at 3:19 Comment(0)
G
0
tmpButtonCtrl.BackColor = DefaultBackColor
tmpButtonCtrl.ForeColor = DefaultForeColor
tmpButtonCtrl.UseVisualStyleBackColor = True

.NET Framework 4.5
VB.Net Visual Studio 2013

Goethe answered 12/6, 2019 at 20:48 Comment(1)
Please edit your answer by adding more explanation, you can also put your code in an indentation, which will make it look nicer.Euphoria

© 2022 - 2024 — McMap. All rights reserved.