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!