Click here to Skip to main content
Licence 
First Posted 11 Jan 2004
Views 65,727
Bookmarked 32 times

Light Color Picker

By | 11 Jan 2004 | Article
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

About the Author

bsargos

Software Developer

France France

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmembermanoj kumar choubey2:33 21 Jan '12  
GeneralMenustrip and color picker Pinmemberredcat2212:54 15 May '07  
Questionthe order of colorlist just as colorRange not ABCD [modified] Pinmemberarbrsoft20:55 7 Mar '07  
QuestionAdding a "none" option PinmemberMark LeMonnier7:18 24 Jan '07  
GeneralColor List customization Pinmemberfhlist3:01 13 Feb '06  
GeneralRe: Color List customization Pinmemberbsargos4:27 13 Feb '06  
QuestionRe: Color List customization PinmemberOlli Nissinen23:18 12 Sep '06  
Hi, I'm not that good in these things so could you please write a bit more exact guide how to do a custom color list. I'm doing this in vb.net and writing the CustomKnownColorCollection class stopped already in first row of your guideline. I did not find IColorCollection anywhere.
AnswerRe: Color List customization PinmemberOlli Nissinen1:04 13 Sep '06  
GeneralThanks Pinmemberidstam2:24 4 Jan '05  
GeneralRe: Thanks Pinmemberbsargos23:17 4 Jan '05  
Generalgreat control Pinmembertakeoffeh15:42 26 Mar '04  
GeneralRe: great control Pinmemberbsargos5:00 29 Mar '04  
Generalwhy not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" PinmemberJcxl20:59 12 Jan '04  
GeneralRe: why not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" Pinmemberbsargos1:12 13 Jan '04  
GeneralRe: why not find this file "ColorPicker.resx" when I open the file "ColorPicker.cs" PinmemberJcxl14:15 13 Jan '04  

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.

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