Click here to Skip to main content
12,761,248 members (30,023 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 11 Jan 2004

Light Color Picker

, 11 Jan 2004
Rate this:
Please Sign up or sign in to vote.
A light Color Picker inherited from ComboBox.

Sample Image - LightColorPicker.jpg


This control is an alternative to the Color Dialog provided by the Framework. It allows the user to quickly select a color among a collection of colors. I was surprised to see that the Framework doesn't provide such a control. I also searched on Code Project some similar controls, but the ones I found didn't please me. In my opinion, they where too heavy. So, I started to write my own. My efforts where concentrated on writing a light and easy-to-use control. Here it is.

The code

Starting from scratch leads to writing much code, and usually provides an incomplete control. I preferred to use existing and working code, and make my ColorPicker inherit from the ComboBox class provided by the Framework. The most important modifications I did are contained in the OnDrawItem function. Actually, this overridden function is required for writing custom ComboBox items as well as the OwnerDrawFixed value for the DrawMode property :

public ColorPicker()
   // Required for owner-draw item
   this.DrawMode = DrawMode.OwnerDrawFixed;
   this.DropDownStyle = ComboBoxStyle.DropDownList;
protected override void OnDrawItem(DrawItemEventArgs e)
   // Draw the background of the item depending on the item state 
   if(e.State == DrawItemState.Selected || e.State == DrawItemState.None) {
   // Draw the item

   // Call the base function

The underlying collection of ColorPicker is a ColorCollection derived-class, which contains all the colors provided by the Framework (System and Web). It implements the IEnumerable interface:

public class KnownColorCollection : ColorCollection
public interface ColorCollection : IEnumerable
   int Count { get; }
   Color this[int i] { get; }
   Color this[string s] { get; }
   IEnumerator GetEnumerator();
   int IndexOf(string ColorName);

I so hid the property Items with the following property:

public new ColorCollection Items
   get { return m_ColorCollection; }
   set {
      if(m_ColorCollection != value && value != null ) {
         m_ColorCollection = value;
         foreach(Color color in value) base.Items.Add(color.Name);

The second property I hid is the SelectedText property, which provides access to the selected color via the SelectedIndexChanged event:

public new string SelectedText
   get { return Items[SelectedIndex].Name; }
   set {
      int selidx = Items.IndexOf(value);
      if(selidx > 0) SelectedIndex = selidx;

Using the control

After creating the object MyColorPicker, you must pass the ColorPicker a ColorCollection in the constructor of your form. There is no default collection.

public ColorPickerTestForm() 

   MyColorPicker.Items = new KnownColorCollection(KnownColorFilter.Web);

If you want, you can specify the color displayed by the ColorPicker by adding this line:

MyColorPicker.SelectedText = "Blue";

To retrieve the selected color, you first need to add this code:

MyColorPicker.SelectedIndexChanged += new EventHandler(MyNotificationFunction);

Then you have to write the corresponding function code in the Form code:

private void MyNotificationFunction(object sender, EventArgs e)
   if(MyColorPicker.SelectedText.Length > 0) {
      this.BackColor = Color.FromName(MyColorPicker.SelectedText);




This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Software Developer
France France
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
manoj kumar choubey21-Jan-12 3:33
membermanoj kumar choubey21-Jan-12 3:33 
GeneralMenustrip and color picker Pin
redcat2215-May-07 13:54
memberredcat2215-May-07 13:54 
Questionthe order of colorlist just as colorRange not ABCD [modified] Pin
arbrsoft7-Mar-07 21:55
memberarbrsoft7-Mar-07 21:55 
QuestionAdding a "none" option Pin
Mark LeMonnier24-Jan-07 8:18
memberMark LeMonnier24-Jan-07 8:18 
GeneralColor List customization Pin
fhlist13-Feb-06 4:01
memberfhlist13-Feb-06 4:01 
GeneralRe: Color List customization Pin
bsargos13-Feb-06 5:27
memberbsargos13-Feb-06 5:27 
QuestionRe: Color List customization Pin
Olli Nissinen13-Sep-06 0:18
memberOlli Nissinen13-Sep-06 0:18 
AnswerRe: Color List customization Pin
Olli Nissinen13-Sep-06 2:04
memberOlli Nissinen13-Sep-06 2:04 
GeneralThanks Pin
idstam4-Jan-05 3:24
memberidstam4-Jan-05 3:24 
GeneralRe: Thanks Pin
bsargos5-Jan-05 0:17
memberbsargos5-Jan-05 0:17 
Generalgreat control Pin
takeoffeh26-Mar-04 16:42
membertakeoffeh26-Mar-04 16:42 
GeneralRe: great control Pin
bsargos29-Mar-04 6:00
memberbsargos29-Mar-04 6:00 
Generalwhy not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" Pin
Jcxl12-Jan-04 21:59
memberJcxl12-Jan-04 21:59 
GeneralRe: why not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" Pin
bsargos13-Jan-04 2:12
memberbsargos13-Jan-04 2:12 
GeneralRe: why not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" Pin
Jcxl13-Jan-04 15:15
memberJcxl13-Jan-04 15:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 12 Jan 2004
Article Copyright 2004 by bsargos
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid