Click here to Skip to main content
Licence CPOL
First Posted 26 Dec 2007
Views 44,209
Downloads 299
Bookmarked 30 times

ReadOnly ComboBox

By | 23 Oct 2008 | Article
An extended ComboBox that adds a ReadOnly property to it.

Sample.JPG

Introduction

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, 
              System.Windows.Forms.VisualStyles.ComboBoxState.Disabled);
            /*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).

License

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

About the Author

Fabio Franco

Software Developer (Senior)
GFT Brazil
Brazil Brazil

Member

Fábio is 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 menu, 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 Development, where he's been working with for the past five years. When he has time, he helps users on MSDN forums and tries to write articles on codeproject. He is still on college as he spent too much time partying, until he finally started to party only on weekends.
 
Fábio works full time and goes to college at night. Despite the baby face, Fábio was born in 83 and is still a party boy.
 
MSDN Profile: http://social.msdn.microsoft.com/profile/f%C3%A1bio%20franco/?type=forum

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Layout  Per page   
  Refresh
GeneralError when Visual Styles not active PinmemberFrazer J8:29 10 Dec '09  
GeneralRe: Error when Visual Styles not active PinmemberFabio Franco2:13 11 Dec '09  
GeneralThankyou Pinmemberger0nim07:23 16 Jun '09  
GeneralRe: Thankyou PinmemberFabio Franco7:28 16 Jun '09  
Questionuse of panel [modified] PinmemberMember 197457020:21 29 Apr '09  
AnswerRe: use of panel PinmemberFabio Franco1:55 30 Apr '09  
QuestionRe: use of panel PinmemberMember 19745702:22 30 Apr '09  
AnswerRe: use of panel PinmemberFabio Franco5:14 30 Apr '09  
GeneralSimply useful PinmemberGio Bejarasco9:44 23 Oct '08  
GeneralRe: Simply useful PinmemberDotDue21:33 23 Oct '08  
GeneralRe: Simply useful PinmemberFabio Franco1:05 24 Oct '08  
GeneralA couple of suggestions PinmemberDotDue22:01 22 Oct '08  
GeneralRe: A couple of suggestions PinmemberFabio Franco0:55 23 Oct '08  
GeneralRe: A couple of suggestions PinmemberDotDue21:31 23 Oct '08  
Generalgood Pinmemberjosephlawrencecarpio16:53 29 Jul '08  
GeneralRe: good PinmemberFabio Franco4:09 30 Jul '08  
GeneralI might be stupid PinmemberJohnny J.5:41 29 Jan '08  
GeneralRe: I might be stupid PinmemberFabio Franco6:14 29 Jan '08  
GeneralRe: I might be stupid PinmemberFabio Franco7:05 29 Jan '08  
GeneralUsing property Pinmemberdfjohn00523:56 3 Jan '08  
GeneralRe: Using property PinmemberFabio Franco0:03 4 Jan '08  
GeneralRe: Using property PinmemberThomas Stockwell8:33 22 Jan '08  
GeneralRe: Using property [modified] PinmemberFabio Franco3:16 24 Jan '08  

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.

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