Click here to Skip to main content
15,879,095 members
Articles / Multimedia / GDI+
Article

ColorComboBox

Rate me:
Please Sign up or sign in to vote.
4.31/5 (19 votes)
11 Aug 20042 min read 142K   1.9K   39   12
Selecting colors with a combo box.

Introduction

The ColorComboBox class has been put together as part of another project I am working on. The idea is obviously that you can change the color of certain things within the application. Unfortunately, the way to do this in most Windows applications is to pop up the standard color dialog box and select a color that you want. I'm sure I'm not the only one who finds this tacky, and thinks there should be a way of doing it that doesn't draw attention away from your application. For this reason, I came up with this class which I feel is a simpler and more elegant way of doing it.

As you can see from the images, there are two ways to use the class: one that doesn't show the text name in the display of the drop down, and one that does. I must admit that when I first wrote the class, I thought it would look better without the text; although, having seen the two side by side, I am now leaning more towards the implementation that includes the text in the drop down.

Implementation

There is nothing really very complicated going on here as the class is just an extension of the standard ComboBox, but I'll just go through the main points of the implementation for the novices. The main area of change is to tell the ComboBox, one, that I wish to take over the drawing of the control, and two, to override the drawing function so that I can implement the drawing in my own way. This is done by:

C#
this.DrawMode = DrawMode.OwnerDrawFixed;

this.DrawItem += new DrawItemEventHandler( OnDrawItem );

which is contained in the constructor of the class. The implementation of the OnDrawItem function is:

C#
Graphics grfx = e.Graphics;
Color brushColor = GetColorFromString( ( string )this.Items[ e.Index ] );
SolidBrush brush = new SolidBrush( brushColor );

grfx.FillRectangle( brush, e.Bounds );

if( bHideText == false )
{
    if( brushColor == Color.Black || brushColor == Color.MidnightBlue
        || brushColor == Color.DarkBlue || brushColor == Color.Indigo
        || brushColor == Color.MediumBlue || brushColor == Color.Maroon
        || brushColor == Color.Navy || brushColor == Color.Purple )
    {
        grfx.DrawString( ( string )this.Items[ e.Index ],
              e.Font, whiteBrush, e.Bounds );
    }
    else
    {
        grfx.DrawString( ( string )this.Items[ e.Index ],
              e.Font, blackBrush, e.Bounds );
    }

    this.SelectionStart = 0;
    this.SelectionLength = 0;
}
else
{
    grfx.DrawString( ( string )this.Items[ e.Index ], e.Font,
        new SolidBrush( GetColorFromString(
          ( string )this.Items[ e.Index ] ) )
          , e.Bounds );
}

The function is called every time that the code is trying to draw a cell item in the drop down ListBox portion of the ComboBox. When the function is called, the code gets the Color that is contained in the ListBox item. As the color data is stored in the ListBox portion of the ComboBox as strings in the normal way, and the code hides the strings, if this is required, by simply changing the forecolor of the item to the same color as the background color.

Once it has the Color it sets the background color to the that color and then draws the rectangle for the ListBox item in the background color. When this is done, the code simply draws the string name of the color to the ListBox item in a way that will be either visible or not, depending on the bool value bHideText.

History

  • 5 October 2003:- Initial release.
  • 9 August 2004:- Fixed painting bug introduced by overriding OnSelectionChangeCommitted.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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

Comments and Discussions

 
Questionbut the scroll bar to the combobox is of default color Pin
ahersn2-Oct-12 23:16
ahersn2-Oct-12 23:16 
GeneralMy vote of 5 Pin
Kanasz Robert27-Sep-12 10:53
professionalKanasz Robert27-Sep-12 10:53 
GeneralMy version of this. Uses Dropdownlist style and much less code (no name refs) [modified] Pin
hannes2222219-Feb-09 15:35
hannes2222219-Feb-09 15:35 
GeneralChange the color combobox Pin
sangram116-May-05 2:54
sangram116-May-05 2:54 
GeneralA little less code a little trick Pin
sylvainbouche26-Jan-05 3:40
sylvainbouche26-Jan-05 3:40 
GeneralRe: A little less code a little trick Pin
James Yang20-Oct-05 21:40
James Yang20-Oct-05 21:40 
GeneralRe: A little less code a little trick Pin
MJDamron12-Dec-05 4:54
MJDamron12-Dec-05 4:54 
GeneralHmm Pin
Anonymous5-Oct-03 17:30
Anonymous5-Oct-03 17:30 
GeneralRe: Hmm Pin
pseudonym675-Oct-03 20:08
pseudonym675-Oct-03 20:08 
GeneralRe: Hmm Pin
Filini8-Oct-03 5:26
Filini8-Oct-03 5:26 
GeneralRe: Hmm Pin
Mike Ellison12-Aug-04 6:28
Mike Ellison12-Aug-04 6:28 
GeneralRe: Hmm Pin
Christopher Scholten8-Nov-05 16:18
professionalChristopher Scholten8-Nov-05 16:18 

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.