Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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.

like image 561
Jason Axelrod Avatar asked Feb 09 '12 15:02

Jason Axelrod


People also ask

Can I use Visual Studio for C?

Visual Studio Code is a lightweight, cross-platform development environment that runs on Windows, Mac, and Linux systems. The Microsoft C/C++ for Visual Studio Code extension supports IntelliSense, debugging, code formatting, auto-completion. Visual Studio for Mac doesn't support Microsoft C++, but does support .

How do I get C in Visual Studio?

Download & Install the C/C++ Extension We need to click on the extension button that displays a sidebar for downloading and installing the C/C++ extension in the visual studio code. In the sidebar, type C Extension. In this image, click on the Install button to install the C/C++ extension.

Is Microsoft Visual C free?

A fully-featured, extensible, free IDE for creating modern applications for Android, iOS, Windows, as well as web applications and cloud services.


2 Answers

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.

like image 81
LarsTech Avatar answered Sep 21 '22 00:09

LarsTech


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.

like image 42
Stefan Paul Noack Avatar answered Sep 22 '22 00:09

Stefan Paul Noack