Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ColorComboBox

0.00/5 (No votes)
11 Aug 2004 1  
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