Click here to Skip to main content
14,979,837 members
Articles / Multimedia / GDI+
Posted 4 Oct 2003


39 bookmarked


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


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.


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:

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:

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 );
        grfx.DrawString( ( string )this.Items[ e.Index ],
              e.Font, blackBrush, e.Bounds );

    this.SelectionStart = 0;
    this.SelectionLength = 0;
    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.


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


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


About the Author

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

Questionbut the scroll bar to the combobox is of default color Pin
ahersn2-Oct-12 23:16
Memberahersn2-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
Memberhannes2222219-Feb-09 15:35 
GeneralChange the color combobox Pin
sangram116-May-05 2:54
Membersangram116-May-05 2:54 
GeneralA little less code a little trick Pin
sylvainbouche26-Jan-05 3:40
Membersylvainbouche26-Jan-05 3:40 
GeneralRe: A little less code a little trick Pin
James Yang20-Oct-05 21:40
MemberJames Yang20-Oct-05 21:40 
GeneralRe: A little less code a little trick Pin
MJDamron12-Dec-05 4:54
MemberMJDamron12-Dec-05 4:54 
GeneralHmm Pin
Anonymous5-Oct-03 17:30
MemberAnonymous5-Oct-03 17:30 
GeneralRe: Hmm Pin
pseudonym675-Oct-03 20:08
Memberpseudonym675-Oct-03 20:08 
GeneralRe: Hmm Pin
Filini8-Oct-03 5:26
MemberFilini8-Oct-03 5:26 
GeneralRe: Hmm Pin
Mike Ellison12-Aug-04 6:28
MemberMike 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.