Quick Tip: Color objects from Hex Codes in .Net

Hex Palette .Net provides a wonderful class called the color class. There are plenty of predefined colors in it that make it easy when you want to set the color of a label or background quickly and easily. Sometimes though, you need to have a little more control on the color you’re working with. Having dabbled in the web realm for so long, hex codes are my friend. So when I discovered I couldn’t simply set the ForeColor property of my label webcontrol using Hex values. I was a bit frustrated and stumped. Fortunately, the Color class itself came to the rescue! They provide a translator class that allows you to easily work hex code into your labels. To start, simply import the System.Drawing library into your class. From there, do something like this:

lblAppName.ForeColor = ColorTranslator.FromHtml("#DEDBEF");

You’ll get a nice shade of light blue, err, off white, err, periwinkle using that code above. What’s really nice is that the ColorTranslator is a shared class, so you don’t even need to instantiate an instance of the object to use it. Enjoy!

What are your 10 bits on the matter? I want to know!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s