A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
Getting the hex value of a color using ColorPickerWith 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) |
||
|
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. ![]() 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: ![]() Using the ColorPicker object and converting RGB to HexAll 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 It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsYour 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. :)
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 :)
Hi! Very good article.
Can you tell me how can i change textcolor? for example in richtext.
thx
This is a great tutorial! oh and internet explorer 8 Beta is out Andrei... might wanna check it out... its pretty cool!
cant open
Good Work
Great you guys!..does anybody know if you can use RGB colors in a C program??
Thx This is What i've ben looking for...
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.
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.
Related Tutorials
Related Source Code
C# Job Search