Getting Back the Control Color. WinForms and Color

I have gotten a couple of questions about how to do thing x with colors in WinForms. One of the weird things that always seem to pop up when working on WinForms is the desire to change the color of a control. The weird thing happens when they want to change it back. I have yet to run into a .NET dev that immediately can answer how to get back. For whatever reason, it is one of those things that seems to slip out of everyone’s heads and require regular look ups. It’s not hard but easily forgotten. As part of another application, I am writing for another question, I have included the quick answer to how to return back to the control color on a control’s background. Lets dig into how to work with color.

Control Background Color Change App

We will start with an app with a single button. This button starts as a control color than if pressed switches to orange.

Control Color before button press
Before Button Press
After Turning Orange
After Button Press

Simple enough. Lets take a look at the source code. Remember doing your code in the code behind is generally frowned on so this is only showing you how to do something and not necessarily the best implementation of doing it.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace ExtraMethodsOnControls
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.BackColor != Color.Orange)
            {
                button1.BackColor = Color.Orange;
            }
            else
            {
                button1.BackColor = SystemColors.Control;
            }
        }
 
       
    }
}

This is really, really simple. If you look in the button1_Click event, you will see logic that says if the button’s back color is not orange, then make it orange. If it is orange then make it the control color. This line is the answer to the question.

If you go into the designer and look at the back color, you get a couple of different options, one is Web, which is set like the line that sets the orange color using the Color object and then you have System, which is accessible using the SystemColors object.

Custom, Web  and System properties.
Designer in the Properties item BackColor

Let review. If you want to set it as Web then use the Color.<YourColor>. If you want one of the system values, use SystemColors.<Your Color>.

It is a nice little reference for everyone who forgets this.

Before we leave lets talk about the last tab in this which is Custom.

Custom Colors

I showed you how to programmatically set all those colors, lets show you how to set a custom color. The easiest way to do this is to set the ARGB values. This means Alpha, Red, Green, Blue.

If you remember from your art class days, you can make any color with those three colors. Alpha is the saturation of that color. If you go to 0 then it will be transparent. If you go to 255 it will be loud and proud.

Each carry a a low value of absent with 0 to the most added to the mix with 255. If you want. Remember the order of the parameters is Alpha, Red, Green and Blue.

Lets look at an example of how to do this.

button1.BackColor = Color.FromArgb(125, 10, 245, 100);

Here we are changing the button1 back color to our custom color. So lets review. This will be about 50 percent saturated as the Alpha is 125. The Red is almost nonexistent at 10. Green is almost at full strength at 245 and the Blue is contributing with 100. Remember each of these values are a scale to 255. Lets see the results.

Custom Color Results

Hopefully that will help the handful of color specific questions you may have. It works the same for ForeColor and most other color properties in Windows Forms.

Leave a Reply

Your email address will not be published.