Click here to Skip to main content
15,867,686 members
Articles / Multimedia / GDI+
Article

Appearance Customizable ComboBox

Rate me:
Please Sign up or sign in to vote.
3.50/5 (5 votes)
27 Mar 20072 min read 91.1K   7.1K   24   10
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


Written By
Singapore Singapore
Ng Pei Jiun, Eugenis, graduated from National University of Singapore. After worked for one year as VB .NET and C# .NET programmer, I had decided to do freelancing in order to experience more about SDLC and Project Management.

I am currently unemployed and having intermediate skill in C# .NET programming.

Comments and Discussions

 
QuestionHow to add functionalities: change background and fore color on hover Pin
Member 150810291-Mar-21 1:48
Member 150810291-Mar-21 1:48 
QuestionDoesn't work with ComboBoxStyle.Simple Pin
karinsue17-Mar-18 5:24
karinsue17-Mar-18 5:24 
QuestionChanging the Control Style to User Paint Pin
Mark Regal28-Sep-12 1:20
Mark Regal28-Sep-12 1:20 
Questionfixed Pin
Member 41579894-May-12 11:53
Member 41579894-May-12 11:53 
fixed
C#
protected override void OnResize(EventArgs e)
      {
          Invalidate();
          base.OnResize(e);

          rectBorder = new Rectangle(0, 0, Width, Height);
          rectDropDownButton = new Rectangle(Width + 100, 0, DropDownButtonWidth, Height);
          gdc = Graphics.FromHdc(hDC);

      }

AnswerRe: fixed Pin
komyoschong12-Aug-12 10:42
komyoschong12-Aug-12 10:42 
GeneralRe: fixed Pin
omegarizla2-Nov-12 0:41
omegarizla2-Nov-12 0:41 
QuestionError on Resize Pin
Member 41579894-May-12 11:17
Member 41579894-May-12 11:17 
QuestionHow to make combobox to drop up instead of drop down? Pin
Shrini4-Sep-07 23:08
Shrini4-Sep-07 23:08 
AnswerRe: How to make combobox to drop up instead of drop down? Pin
superfly713-Nov-16 20:37
professionalsuperfly713-Nov-16 20:37 
Questioncan you build this in in 2003? Pin
honpher25-May-07 5:23
honpher25-May-07 5:23 

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.