Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / multimedia / GDI+

Appearance Customizable ComboBox

3.50/5 (5 votes)
27 Mar 20072 min read 1   7.2K  
A class implementing .Net ComboBox Control that allows appearance customization.
Screenshot - demo.gif

Introduction

This class is derived from the .NET ComboBox class. It provides coloring and stylish customization to change the appearance of ComboBox's border and drop down button. An independent set of properties can be customized for both the GotFocus and LostFocus statuses. These properties are customizable from within the designer.

The original ComboBox control provided by .NET 2.0 Framework is very limited in the ability to customize the appearance. A lot of people have created their own custom made ComboBox instead of deriving from the ComboBox from .NET Framework. It might be due to the OnPaint function of the .NET ComboBox being inaccessible to the text box area of the ComboBox. You can allow overriding of the OnPaint function by using this statement in the constructor:

C#
public CustomComboBox() : base()
{
    SetStyle(ControlStyles.UserPaint, true);
}

An alternative to re-drawing the ComboBox control is by overriding the WndProc function. However this approach can be significantly complicated to many beginners of C#, .NET or Win32 programming, especially when they want to continually select the suitable color and style to match the design of their application.

Background

This project is extended from the existing project, "Making Standard ComboBox appear flat" contributed by Fadrian Sudaman. I am impressed with what has been done by Fadrian Sudaman to draw on top of .NET ComboBox control by using WndProc.

Using the code

This class inherits from .NET ComboBox.

Instead of using:

C#
ComboBox cboDemo = new ComboBox();

You can use:

C#
ComboBox cboDemo = new CustomComboBox();
// or
CustomComboBox cboDemo = new CustomComboBox();

To use this class, reader needs to understand the standard functions, properties and events of .NET ComboBox, and a few new properties created in this inherited class.

Sample Image - maximum width is 600 pixels

Points of Interest

I have spent a lot of time to fix the flickering problem of this inherited control. I cut off a lot of unnecessary codes and simplified out the overridden WndProc function. I also changed the several "OnEvent" function and moved the Invalidate() to before base.OnEvent(e).

It still does flicker, especially if I set my video acceleration to None. However, I believe this should have minimized flickering into an acceptable level.

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