Click here to Skip to main content
13,147,037 members (63,276 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 14 Jul 2011

Quick 'n' easy Colour Combo Box

, 15 Jul 2011
Rate this:
Please Sign up or sign in to vote.
Full code listing for a very simple but effective Colour-Combo (Color for those in America)
In the Visual Studio forms designer, the property grids allow you to pick a colour from a drop-down box.
I was a little surprised to find that there was no existing control that already had this functionality.

A search on the internet turned up a number of results, but they all seemed to have over complicated the problem, and were hundreds of lines of code or they used the tool-strip drop down.

This little control extension sets the OwnerDrawFixed property to true, this activates the DrawItem event and the OnDrawItem method (if you override the OnDrawItem method, but don't set the control to be OwnerDrawFixed, then OnDrawItem is never invoked, and the DrawItem event is never called).

The OnDrawItem method is overridden, and if the item that is being drawn is a System.Drawing.Color struct, then the method manufactures a new DrawItemEventArgs object, with the BackgroundColor property set to the current item colour.

The DrawItem event is handled, and in this event we simply duplicate what the standard event did, which is to draw the background and the text value over it.

/// <summary>
/// extends the ComboBox to offer a Colour-Combo (one that allows you to select colours from a drop-down)
/// </summary>
class ColourCombo : ComboBox
    public ColourCombo()
        : base()
        // set draw mode to OwnerDrawFixed to allow custom drawing of the combo-box items.
        this.DrawMode = DrawMode.OwnerDrawFixed;
        this.DrawItem += new DrawItemEventHandler(ColourCombo_DrawItem);
    /// <summary>
    /// handle the DrawItem event to provide a custom draw method.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void ColourCombo_DrawItem(object sender, DrawItemEventArgs e)
        // draw the background in the specified background colour.
        // draw the text:
        e.Graphics.DrawString(((ComboBox)sender).Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
    /// <summary>
    /// set the back colour to the currently selected colour, and set the fore colour to black or white, depending on the
    /// brightness of the selected colour.
    /// </summary>
    /// <param name="e"></param>
    protected override void OnSelectedIndexChanged(EventArgs e)
        this.BackColor = this.SelectedColour;
        if (this.BackColor.GetBrightness() < 0.5)
            this.ForeColor = Color.White;
            this.ForeColor = Color.Black;
    /// <summary>
    /// override the OnDrawItem method to generate custom DrawItemEventArgs.
    /// </summary>
    /// <param name="e"></param>
    protected override void OnDrawItem(DrawItemEventArgs e)
        // fetch the list-item to be drawn:
        var item = this.Items[e.Index];
        // is this a colour struct?
        if (item is Color)
            // generate a new DrawItemEventArgs, with the selected colour as the back-colour.
            DrawItemEventArgs de = new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State, e.ForeColor, (Color)item);
    /// <summary>
    /// load the standard ROYGIBV colour wheel into the colour combo.
    /// </summary>
    public void LoadRainbow()
    /// <summary>
    /// load up a darker version of the ROYGBIV colour wheel.
    /// </summary>
    public void LoadDarkRainbow()
    /// <summary>
    /// gets or sets the selected colour.
    /// </summary>
    public Color SelectedColour
            if (SelectedItem != null && SelectedItem is Color)
                return (Color)SelectedItem;
                return this.BackColor;
            this.SelectedItem = value;


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


About the Author

Simon Bridge
Software Developer (Senior) Decipha
Australia Australia
Wrote his first computer game in Microsoft Basic, on a Dragon 32 at age 7. It wasn't very good.
Has been working as a consultant and developer for the last 15 years,
Discovered C# shortly after it was created, and hasn't looked back.
Feels weird talking about himself in the third person.

You may also be interested in...


Comments and Discussions

QuestionHow to use this class in my code ? Where is Form1_Load() ? Pin
Member 1204306814-Oct-15 22:56
memberMember 1204306814-Oct-15 22:56 
GeneralA rare moment in programming to see colour properly spelt, t... Pin
TetheredSun18-Jul-11 22:06
memberTetheredSun18-Jul-11 22:06 
GeneralNice :) When adding the colors to the items in the combobox... Pin
CaptainBriBrian15-Jul-11 3:36
memberCaptainBriBrian15-Jul-11 3:36 
GeneralI like it. Well done! Pin
johannesnestler14-Jul-11 22:48
memberjohannesnestler14-Jul-11 22:48 

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.170915.1 | Last Updated 15 Jul 2011
Article Copyright 2011 by Simon Bridge
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid