Deborah's Developer MindScape

         Tips and Techniques for Web and .NET developers.

August 17, 2010

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

Filed under: C#,VB.NET,WinForms @ 11:45 pm

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.



  1.   Richard — August 18, 2010 @ 10:05 am    Reply

    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)

  2.   DeborahK — August 18, 2010 @ 10:05 pm    Reply

    Cool. I had not tried that.

    Thanks Richard!

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2020 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs