Setting WinForms Colors to a Named, RGB, or Hex Value

Posted Tue, Aug 17 2010 21:45 by Deborah Kurata

Sometimes you want to add a little color to your WinForms controls. WinForms comes with a large set of named colors that you can use.

NOTE: Be sure to import a reference to System.Drawing.

For example

In C#:

textBox1.BackColor = Color.Linen;

In VB:

textBox1.BackColor = Color.Linen

But what if you want to display a color that is not one of the named colors? Then you can use the Color.FromArgb method to define your color by specifying the red, green, and blue values.

In C#:

textBox1.BackColor = Color.FromArgb(250, 245, 235);

In VB:

textBox1.BackColor = Color.FromArgb(250, 245, 235)

Ah, but what if you have the color defined as a hexadecimal value? Well, you could use Bing/Google to find a converter to convert it to RGB. Or you can use the ColorTranslator.FromHtml method.

In C#:

textBox1.BackColor = ColorTranslator.FromHtml("#FAF9F9");

In VB:

textBox3.BackColor = ColorTranslator.FromHtml("#FAF9F9")

Use any of these techniques to add some color to your WinForms applications.

Enjoy!

Filed under: , , , ,

Comments

# re: Setting WinForms Colors to a Named, RGB, or Hex Value

Wednesday, August 18, 2010 10:05 AM by Richard

Or, alternatively:

In C#:

textBox1.BackColor = Color.FromArgb(0xFAF9F9);

textBox1.BackColor = Color.FromArgb(0xFA, 0xF9, 0xF9);

In VB:

textBox3.BackColor = Color.FromArgb(&HFAF9F9)

textBox3.BackColor = Color.FromArgb(&HFA, &HF9, &HF9)

# re: Setting WinForms Colors to a Named, RGB, or Hex Value

Wednesday, August 18, 2010 10:05 PM by Deborah Kurata

Cool. I had not tried that.

Thanks Richard!

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: