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

ColorComboBox

, 11 Aug 2004
Rate this:
Please Sign up or sign in to vote.
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:

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 );
    }
    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

Share

About the Author

pseudonym67

United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
Questionbut the scroll bar to the combobox is of default color Pinmemberahersn3-Oct-12 0:16 
GeneralMy vote of 5 PinmvpKanasz Robert27-Sep-12 11:53 
GeneralMy version of this. Uses Dropdownlist style and much less code (no name refs) [modified] Pinmemberhannes2222219-Feb-09 16:35 
GeneralChange the color combobox Pinmembersangram116-May-05 3:54 
GeneralA little less code a little trick Pinmembersylvainbouche26-Jan-05 4:40 
GeneralRe: A little less code a little trick PinmemberJames Yang20-Oct-05 22:40 
GeneralRe: A little less code a little trick PinmemberMJDamron12-Dec-05 5:54 
GeneralHmm PinsussAnonymous5-Oct-03 18:30 
GeneralRe: Hmm Pinmemberpseudonym675-Oct-03 21:08 
GeneralRe: Hmm PinmemberFilini8-Oct-03 6:26 
GeneralRe: Hmm PinmemberMike Ellison12-Aug-04 7:28 
GeneralRe: Hmm PinmemberChristopher Scholten8-Nov-05 17:18 

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
Web04 | 2.8.141223.1 | Last Updated 12 Aug 2004
Article Copyright 2003 by pseudonym67
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid