Click here to Skip to main content
11,920,156 members (58,007 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked

ReadOnly ComboBox

, 23 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
An extended ComboBox that adds a ReadOnly property to it.



I decided to create a new combobox based on requests from the users of MSDN Windows Forms forum. It adds a new property to the regular ComboBox so it can be set to read-only, much like the regular TextBoxes.

Using the code

I made this code pretty straightforward so all you have to do is download the C# file enclosed in the ZIP file above and add it to your project. Build it, and the ExComboBox should be available on the Toolbox.

You can change the way the disabled button looks by commenting the ComboBoxRenderer line and uncommenting the commented portion of the code. You can set the color of the gradient that makes the button of the read-only mode by changing the colors of the LinearGradientBrush variable lgb on the OnPaint method:

protected class DblPanel : Panel
    protected override void OnPaint(PaintEventArgs e)
        if (this.Visible == true)
            ComboBoxRenderer.DrawDropDownButton(e.Graphics, e.ClipRectangle, 
            /*Pen pen = new Pen(Color.DarkGray);
            Pen penBR = new Pen(Color.LightGray);
            Pen penArrow = new Pen(Color.LightGray);
            penArrow.Width = 2;
            penArrow.EndCap = LineCap.Square;
            Graphics g = e.Graphics;
            LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), 
                                      new Point(0, this.Height + 1), 
                                      Color.LightGray, Color.Gray);

Points of interest

I learned a bit while trying to add the read-only feature to the regular ComboBox. It is interesting that the combobox does not have any sub-controls in it (check the ComboBox.Controls.Count property and you will see it is 0); all is done by custom drawing. And, I also learned that there is no way to change this by overriding its Paint methods as it doesn't happen there; and, I couldn't figure out where it draws itself, so I used a Panel to draw over the regular dropdown button.

So basically, all the functionality is kept from the original combo box except the ability for the user to change the selected item without disabling the control (many complained that setting the Enabled property to false made it difficult to read and didn't allow the use of custom colors).


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Fabio Franco
Founder Actus Sistemas
Brazil Brazil
Fábio used to be a party boy that for some reason fell into the geek world. Perhaps his taste for coding came as a consequence of his taste for games. His first program was a batch file for MS-DOS at the age of 14. The need to make it easier to start the games had him do a games launcher, with password to blackmail his younger brothers.

After that his taste for programming only grew, at the age of 16 Fábio accidentaly stumbled in a program that makes programs, a.k.a. Visual Basic 5.
From there he self taught, through books, how to code in VB 5, 6 and later C, C++ and finally C#.

Currently Fábio specializes in C# Desktop and Web Development, that he's been working with since 2004. When he has time, he helps users on MSDN forums and tries to write articles on codeproject.

MSDN Profile:

You may also be interested in...

Comments and Discussions

GeneralError when Visual Styles not active Pin
Frazer J10-Dec-09 9:29
memberFrazer J10-Dec-09 9:29 
GeneralRe: Error when Visual Styles not active Pin
Fabio Franco11-Dec-09 3:13
memberFabio Franco11-Dec-09 3:13 
GeneralThankyou Pin
ger0nim016-Jun-09 8:23
memberger0nim016-Jun-09 8:23 
GeneralRe: Thankyou Pin
Fabio Franco16-Jun-09 8:28
memberFabio Franco16-Jun-09 8:28 
Questionuse of panel [modified] Pin
Member 197457029-Apr-09 21:21
memberMember 197457029-Apr-09 21:21 

You added a panel to your ComboBox control, what r u trying to do there?
r u using the panel to "mask" the control in order to support "ReadOnly"? Correct me if I am wrong.


modified on Thursday, April 30, 2009 7:51 AM

AnswerRe: use of panel Pin
Fabio Franco30-Apr-09 2:55
memberFabio Franco30-Apr-09 2:55 
QuestionRe: use of panel Pin
Member 197457030-Apr-09 3:22
memberMember 197457030-Apr-09 3:22 
AnswerRe: use of panel Pin
Fabio Franco30-Apr-09 6:14
memberFabio Franco30-Apr-09 6:14 
GeneralSimply useful Pin
Gio Bejarasco23-Oct-08 10:44
memberGio Bejarasco23-Oct-08 10:44 
GeneralRe: Simply useful Pin
DotDue23-Oct-08 22:33
memberDotDue23-Oct-08 22:33 
GeneralRe: Simply useful Pin
Fabio Franco24-Oct-08 2:05
memberFabio Franco24-Oct-08 2:05 
GeneralA couple of suggestions Pin
DotDue22-Oct-08 23:01
memberDotDue22-Oct-08 23:01 
GeneralRe: A couple of suggestions Pin
Fabio Franco23-Oct-08 1:55
memberFabio Franco23-Oct-08 1:55 
GeneralRe: A couple of suggestions Pin
DotDue23-Oct-08 22:31
memberDotDue23-Oct-08 22:31 
Generalgood Pin
josephlawrencecarpio29-Jul-08 17:53
memberjosephlawrencecarpio29-Jul-08 17:53 
GeneralRe: good Pin
Fabio Franco30-Jul-08 5:09
memberFabio Franco30-Jul-08 5:09 
GeneralI might be stupid Pin
Johnny J.29-Jan-08 6:41
memberJohnny J.29-Jan-08 6:41 
GeneralRe: I might be stupid Pin
Fabio Franco29-Jan-08 7:14
memberFabio Franco29-Jan-08 7:14 
GeneralRe: I might be stupid Pin
Fabio Franco29-Jan-08 8:05
memberFabio Franco29-Jan-08 8:05 
GeneralUsing property Pin
dfjohn0054-Jan-08 0:56
memberdfjohn0054-Jan-08 0:56 
GeneralRe: Using property Pin
Fabio Franco4-Jan-08 1:03
memberFabio Franco4-Jan-08 1:03 
GeneralRe: Using property Pin
Thomas Stockwell22-Jan-08 9:33
memberThomas Stockwell22-Jan-08 9:33 
GeneralRe: Using property [modified] Pin
Fabio Franco24-Jan-08 4:16
memberFabio Franco24-Jan-08 4:16 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151120.1 | Last Updated 23 Oct 2008
Article Copyright 2007 by Fabio Franco
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid