Click here to Skip to main content
15,867,453 members
Articles / Desktop Programming / Windows Forms

Color Picker Combo Box

Rate me:
Please Sign up or sign in to vote.
4.89/5 (15 votes)
20 Mar 2009CPOL2 min read 120.9K   7.4K   22   8
Using the ComboBox control as a color picker.

Introduction

This is a short article on how to use the ComboBox control as a color picker to display and select colors. A small strip of the color and its name for all the known colors is shown in the drop down of the ComboBox control as shown below:

Background

The ComboBox control has a DrawItem event which needs to be implemented in the code to achieve this. There is a property called DrawMode for the ComboBox control which determines whether the Operating System or the code will handle the drawing of the items in the list. This property must be set to ‘OwnerDrawFixed’ using the Properties window in order for the DrawItem event implementation to be called.

Using the code

The solution contains only a single project, and that project contains a single form. On the form, I have a button, the click of which will populate the combo box with all the named colors in the Color struct. I am using the selected color for changing the background color of a panel on the form.

The button click implementation is as shown below. I have used Reflection to get a collection of all the colors in the System.Drawing.Color structure. The color names are then added to the combo box.

C#
//
// Button click event handler
//

private void btnLoad_Click(object sender, EventArgs e)
{
    ArrayList ColorList = new ArrayList();
    Type colorType = typeof(System.Drawing.Color);
    PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | 
                                  BindingFlags.DeclaredOnly | BindingFlags.Public);
    foreach (PropertyInfo c in propInfoList)
    {
        this.cmbboxClr.Items.Add(c.Name);
    }
}

In the ComboBox control’s DrawItem event, the Graphics object (which can be obtained through the Graphics property of the DrawItemEventArgs) is used to draw a strip of the named color using its FillRectangle method. The DrawString method is used to add the name of the color. The DrawItem event will be triggered for each item added to the combo box.

C#
//
// DrawItem event  handler
//

private void cmbboxClr_DrawItem(object sender, DrawItemEventArgs e)
{
    Graphics g = e.Graphics;
    Rectangle rect = e.Bounds;
    if (e.Index >= 0)
    {
       string n = ((ComboBox)sender).Items[e.Index].ToString();
       Font f = new Font("Arial", 9, FontStyle.Regular);
       Color c = Color.FromName(n);
       Brush b = new SolidBrush(c);
       g.DrawString(n, f, Brushes.Black, rect.X, rect.Top);
       g.FillRectangle(b, rect.X + 110, rect.Y + 5, 
                       rect.Width -10, rect.Height - 10);
    }
}

A snapshot of the sample form:

You can download the full project and open it in Visual Studio 2005 for more details.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
Questionmouseover changing font to bold Pin
Bear Redbourn7-May-19 5:53
Bear Redbourn7-May-19 5:53 
QuestionVery Useful. Thank you Pin
Chris J-1378676018-Apr-18 12:06
professionalChris J-1378676018-Apr-18 12:06 
QuestionVS 2013 does not populate comboBox Pin
rfresh4-Oct-17 5:54
rfresh4-Oct-17 5:54 
GeneralMy vote of 5 Pin
Best_200622-Sep-16 21:23
Best_200622-Sep-16 21:23 
QuestionThanks, I rewrite your code into VB .NET Pin
chovan_m21-Jun-15 3:09
chovan_m21-Jun-15 3:09 
News这是我修改的代码 Pin
roman_201223-Jun-14 22:33
roman_201223-Jun-14 22:33 
Suggestione.DrawBackground(); Pin
roman_201223-Jun-14 19:06
roman_201223-Jun-14 19:06 
GeneralMy vote of 5 Pin
ambarishtv13-Feb-12 2:25
ambarishtv13-Feb-12 2:25 

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.