Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Quick 'n' easy Colour Combo Box

5.00/5 (2 votes)
15 Jul 2011CPOL 27.2K  
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)