Click here to Skip to main content
Click here to Skip to main content
Go to top

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

Introduction

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) {
      e.DrawBackground();
   }
   
   // Draw the item
   ...

   // Call the base function
   base.OnDrawItem(e);
}

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);

   }

}

License

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

Share

About the Author

bsargos
Software Developer
France France
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 Pinmembermanoj kumar choubey21-Jan-12 2:33 
GeneralMenustrip and color picker Pinmemberredcat2215-May-07 12:54 
Questionthe order of colorlist just as colorRange not ABCD [modified] Pinmemberarbrsoft7-Mar-07 20:55 
QuestionAdding a "none" option PinmemberMark LeMonnier24-Jan-07 7:18 
GeneralColor List customization Pinmemberfhlist13-Feb-06 3:01 
GeneralRe: Color List customization Pinmemberbsargos13-Feb-06 4:27 
QuestionRe: Color List customization PinmemberOlli Nissinen12-Sep-06 23:18 
AnswerRe: Color List customization PinmemberOlli Nissinen13-Sep-06 1:04 
GeneralThanks Pinmemberidstam4-Jan-05 2:24 
GeneralRe: Thanks Pinmemberbsargos4-Jan-05 23:17 
Generalgreat control Pinmembertakeoffeh26-Mar-04 15:42 
GeneralRe: great control Pinmemberbsargos29-Mar-04 5:00 
Generalwhy not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" PinmemberJcxl12-Jan-04 20:59 
GeneralRe: why not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" Pinmemberbsargos13-Jan-04 1:12 
GeneralRe: why not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" PinmemberJcxl13-Jan-04 14:15 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 12 Jan 2004
Article Copyright 2004 by bsargos
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid