Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Quick 'n' easy Colour Combo Box

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
15 Jul 2011CPOL 26.4K   4   4
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.

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


Written By
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.

Comments and Discussions

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