Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Article

Light Color Picker

Rate me:
Please Sign up or sign in to vote.
4.53/5 (24 votes)
11 Jan 20041 min read 88.9K   1K   34   15
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 :

C#
public ColorPicker()
{
   // Required for owner-draw item
   this.DrawMode = DrawMode.OwnerDrawFixed;
   this.DropDownStyle = ComboBoxStyle.DropDownList;
}
C#
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:

C#
public class KnownColorCollection : ColorCollection
{
   ...
}
C#
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:

C#
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:

C#
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.

C#
public ColorPickerTestForm() 
{
   ...

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

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

C#
MyColorPicker.SelectedText = "Blue";

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

C#
MyColorPicker.SelectedIndexChanged += new EventHandler(MyNotificationFunction);

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

C#
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


Written By
Software Developer
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Manoj Kumar Choubey21-Jan-12 2:33
professionalManoj Kumar Choubey21-Jan-12 2:33 
GeneralMenustrip and color picker Pin
redcat2215-May-07 12:54
redcat2215-May-07 12:54 
Questionthe order of colorlist just as colorRange not ABCD [modified] Pin
arbrsoft7-Mar-07 20:55
arbrsoft7-Mar-07 20:55 
QuestionAdding a "none" option Pin
Mark LeMonnier24-Jan-07 7:18
Mark LeMonnier24-Jan-07 7:18 
GeneralColor List customization Pin
fhlist13-Feb-06 3:01
fhlist13-Feb-06 3:01 
GeneralRe: Color List customization Pin
bsargos13-Feb-06 4:27
bsargos13-Feb-06 4:27 
QuestionRe: Color List customization Pin
Olli Nissinen12-Sep-06 23:18
Olli Nissinen12-Sep-06 23:18 
AnswerRe: Color List customization Pin
Olli Nissinen13-Sep-06 1:04
Olli Nissinen13-Sep-06 1:04 
GeneralThanks Pin
idstam4-Jan-05 2:24
idstam4-Jan-05 2:24 
GeneralRe: Thanks Pin
bsargos4-Jan-05 23:17
bsargos4-Jan-05 23:17 
Generalgreat control Pin
takeoffeh26-Mar-04 15:42
takeoffeh26-Mar-04 15:42 
GeneralRe: great control Pin
bsargos29-Mar-04 5:00
bsargos29-Mar-04 5:00 
Generalwhy not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" Pin
Jcxl12-Jan-04 20:59
Jcxl12-Jan-04 20:59 
GeneralRe: why not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" Pin
bsargos13-Jan-04 1:12
bsargos13-Jan-04 1:12 
GeneralRe: why not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" Pin
Jcxl13-Jan-04 14:15
Jcxl13-Jan-04 14: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.