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
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
lgb on the
protected class DblPanel : Panel
protected override void OnPaint(PaintEventArgs e)
if (this.Visible == true)
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).
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 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: http://social.msdn.microsoft.com/profile/f%C3%A1bio%20franco/?type=forum