Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Getting the hex value of a color using ColorPicker

With the .NET Framework you can use the Windows Color Picker control in your application thanks to the ColorPicker object. However, the ColorPicker object doesn't have a method or property to get the color in hex format. In this tutorial we're going to build a color picker application and we will be getting the hex value of the selected color.

On Sunday, May 7th 2006 at 04:15 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.6 with 14 votes)
Contextual Ads
More C# Resources
Advertisement
Download this Visual Studio 2005 project Download this project (Visual Studio 2005)

I always hated to open Photoshop for the simple task of picking a color when I needed it in for using in a website, most of the time in CSS files. The Windows color picker doesn't show me the hex value of the color, only the RGB (Red Green Blue) values of the color. So instead of searching Google like a normal person to download such program, I decided to give it 5 minutes and build my own.

Hex Color Picker

Start by creating a new Windows Application project in Visual Studio 2005 and add to the form a Button btnPick for launching the Windows color picker, a PictureBox picColor for displaying the picked color, and four textboxes txtHex, txtRed, txtGreen, txtBlue which will show the values of the color. Now set the BackColor property of the PictureBox to a color of your own, I selected a blue color (0, 0, 192 in RGB). This will be the default selected color when the application loads.
Additionally you can add labels so that you make it look like below:

Color Picker Form

Using the ColorPicker object and converting RGB to Hex

All the code we'll be using in this tutorial will be placed inside the Click event of the btnPick button. You can get Visual Studio 2005 to create this event handler for you by double clicking the button in Visual Studio Form Designer view. Now that you're inside btnPick_Click paste the following code:


// Set the default picked color to the current color of the PictureBox

clrPicker.Color = picColor.BackColor;

// If we want to show the full color pallet for creating custom colors

clrPicker.FullOpen = true;

 

// Show the dialog, and if OK is pressed

if (clrPicker.ShowDialog() == DialogResult.OK)

{

   // Set the background color of the PictureBox to the picked color

   picColor.BackColor = clrPicker.Color;

   // Store the color as a hex number

   string HexColor = string.Format("0x{0:X8}", clrPicker.Color.ToArgb());

   // Strip the unnecessary characters at the end of the hex number

   txtHex.Text = "#" + HexColor.Substring(HexColor.Length - 6, 6);

 

   // Set the RGB textboxes

   txtRed.Text = clrPicker.Color.R.ToString();

   txtGreen.Text = clrPicker.Color.G.ToString();

   txtBlue.Text = clrPicker.Color.B.ToString();

}


The code is pretty easy to figure out, especially due to the comments so I don't think it needs further explanation, but if you have questions go ahead and ask.
The important part of the code is in converting the color from RGB to hex using string.Format(), however we are also displaying the Red, Green and Blue values that compose the color in case anyone needs to retrieve this in their application.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Daisy on Monday, June 5th 2006 at 10:19 AM

Your website is cool but it makes my internet explorer 6 with service pack 2 crash because of annoying pop-ups even if I had the pop up blocker on. (the smiley's ) I hate them, Please make a better way to display your ads that are not annoying. Please make respect with this beautiful lady. :)

by Andrei Pociu on Monday, June 5th 2006 at 11:50 AM

Thank you for your feedback, I do consider removing the pop-up ads. I don't experience such crashes, but I believe they are related to a bug in the Macromedia Flash plugin.

Oh, and give Internet Explorer 7 Beta 2 a try :)

by Attila Turóczy on Thursday, July 27th 2006 at 03:04 PM

Hi! Very good article.
Can you tell me how can i change textcolor? for example in richtext.
thx

by VGStudios Owner on Friday, June 6th 2008 at 02:47 PM

This is a great tutorial! oh and internet explorer 8 Beta is out Andrei... might wanna check it out... its pretty cool!

by # on Wednesday, July 2nd 2008 at 09:32 AM

cant open

by Girish on Wednesday, September 3rd 2008 at 10:37 PM

Good Work

by tinat on Wednesday, December 17th 2008 at 08:18 AM

Great you guys!..does anybody know if you can use RGB colors in a C program??

by Aaron on Wednesday, December 31st 2008 at 01:17 AM

Thx This is What i've ben looking for...

by Ed Brown on Wednesday, September 23rd 2009 at 03:50 PM

Nice work. Simple, effective tool for getting the colors without pulling up some other memory hog app. Will use it as a regular css tool for my web apps.

by Ed Brown on Wednesday, September 23rd 2009 at 03:50 PM

Nice work. Simple, effective tool for getting the colors without pulling up some other memory hog app. Will use it as a regular css tool for my web apps.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources