Jason Axelrod
Jason Axelrod

Reputation: 7805

Visual Studio C# Windows Forms... changing button color?

I have a form (see screenshot):

default form look

As you can see, its a pretty basic form, with a save button. I have programmed it so that if any of the text fields get changed, the "SAVE" button changes color so that its obvious that I haven't clicked save and don't forget to. Unfortunately, simply changing the BackColor of the button to red isn't enough, because its UGLY as sin.

ugly backcolor unsaved

What can I do to change the color of the button to red, but not as ugly. As you can see, the "BackColor" doesn't change the entire button, just the inner piece. The border is still the same old fashioned transparent grey.

Upvotes: 5

Views: 23126

Answers (5)

KyleTheGolfer
KyleTheGolfer

Reputation: 11

You can just use one of the button's properties, "FlatStyle". By default, it is standard. But if you switch it to "Popup", your background color will be extended to the area of the button. You can compare the followings:

left-standard, right-popup

Upvotes: 1

LarsTech
LarsTech

Reputation: 81610

A little bit of a LinearGradientBrush can go a long way to soften the harshness of a pure red button.

button1.ForeColor = Color.White;

Bitmap bmp = new Bitmap(button1.Width, button1.Height);
using (Graphics g = Graphics.FromImage(bmp)) {
  Rectangle r = new Rectangle(0, 0, bmp.Width, bmp.Height);
  using (LinearGradientBrush br = new LinearGradientBrush(
                                      r,
                                      Color.Red,
                                      Color.DarkRed,
                                      LinearGradientMode.Vertical)) {
      g.FillRectangle(br, r);
    }
  }

then you can just assign the image to the button's BackgroundImage property:

  button1.BackgroundImage = bmp;

Result:

enter image description here

Note: Assigning a background image will lose the mouse hover coloring of the button.

Upvotes: 12

emmalyx
emmalyx

Reputation: 2356

This won't work in WinForms, but you might want to switch over to WPF. It's much more convenient because you can configure EVERYTHING. (Even the color of a progressbar)

Edit: The OP doesn't have to entirely rewrite his application. He just needs to redo the layout of it, the code can be C&P'd over to the new WPF project, fyi

Edit²: You don't even need to use code to change the color of a WPF button, you can just define a red overlay with 30% opacity in the XAML file. It's that easy, really.

Upvotes: -1

Stefan Paul Noack
Stefan Paul Noack

Reputation: 3734

Another solution would be to add an Icon (e.g. exclamation mark) to the button instead to inform the user that the changes haven't been saved yet.

Upvotes: 2

bobek
bobek

Reputation: 8020

There are many tutorials online on how to create nice buttons with c#. For example this one allows you to create Vista like buttons. Have a look here: http://www.codeproject.com/Articles/19318/Vista-Style-Button-in-C

For basic colors visit this SO question:

C#: Changing Button BackColor has no effect

Upvotes: 1

Related Questions