Click here to Skip to main content
Click here to Skip to main content

Quick 'n' easy Colour Combo Box

, 15 Jul 2011 CPOL
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.
        e.DrawBackground();
 
        // 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)
    {
        base.OnSelectedIndexChanged(e);
        this.BackColor = this.SelectedColour;
        if (this.BackColor.GetBrightness() < 0.5)
            this.ForeColor = Color.White;
        else
            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);
            base.OnDrawItem(de);
        }
        else
            base.OnDrawItem(e);
    }
 
    /// <summary>
    /// load the standard ROYGIBV colour wheel into the colour combo.
    /// </summary>
    public void LoadRainbow()
    {
        this.Items.Add(Color.Red);
        this.Items.Add(Color.Orange);
        this.Items.Add(Color.Yellow);
        this.Items.Add(Color.Green);
        this.Items.Add(Color.Blue);
        this.Items.Add(Color.Indigo);
        this.Items.Add(Color.Violet);
    }
 
    /// <summary>
    /// load up a darker version of the ROYGBIV colour wheel.
    /// </summary>
    public void LoadDarkRainbow()
    {
        this.Items.Add(Color.DarkRed);
        this.Items.Add(Color.DarkOrange);
        this.Items.Add(Color.Brown);
        this.Items.Add(Color.DarkGreen);
        this.Items.Add(Color.DarkBlue);
        this.Items.Add(Color.DarkMagenta);
        this.Items.Add(Color.DarkViolet);
    }
 
    /// <summary>
    /// gets or sets the selected colour.
    /// </summary>
    public Color SelectedColour
    {
        get
        {
            if (SelectedItem != null && SelectedItem is Color)
                return (Color)SelectedItem;
            else
                return this.BackColor;
        }
        set
        {
            this.SelectedItem = value;
        }
    }
}

License

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

Share

About the Author

Simon Bridge
Technical Lead Evolution Management Consulting
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# a few years ago and hasn't looked back.
Feels wierd talking about himself in the third person.

Comments and Discussions

 
GeneralA rare moment in programming to see colour properly spelt, t... PinmemberTetheredSun18-Jul-11 23:06 
GeneralNice :) When adding the colors to the items in the combobox... PinmemberCaptainBriBrian15-Jul-11 4:36 
GeneralI like it. Well done! Pinmemberjohannesnestler14-Jul-11 23:48 

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 | Terms of Use | Mobile
Web03 | 2.8.150129.1 | Last Updated 15 Jul 2011
Article Copyright 2011 by Simon Bridge
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid