Click here to Skip to main content
13,298,551 members (52,894 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked
Posted 6 Aug 2006

A combobox that looks decent when it is disabled

, 6 Aug 2006
Rate this:
Please Sign up or sign in to vote.
This article describes a method to ownerdraw a combo box so that the text is readable when the combobox is disabled.


One of the things I have never liked about the combobox is that it is almost unreadable when it is disabled. I have an application that does a lot of databinding and enables or disables controls behind the scenes based upon access rules. The comboboxes show the right values, but it is grey on beige, and very hard to read. I don't want to make them textboxes because it would make my code considerably more complex (the GUI is generated at runtime).

I did a lot of research on the web and didn't find too many articles that dealt specifically with changing the appearance of the edit box. There are lots of articles that show an ownerdrawn list, but that only is helpful when the listbox is enabled.

What it looks like

As you can see in the first entry, the disabled combo is pretty ugly. The second entry shows my control in a disabled state - much more readable! The third entry shows that the control works normally when enabled.

I implemented the code as a simple subclass from combobox, so it can be dropped in as a replacement anywhere a combobox is used.

Key parts of the code

First, to allow your combo to be owner drawn, you have to add a DrawItemEventHandler and also set the DrawMode to OwnerDrawFixed. You can use OwnerDrawVariable if you want to change the size of the list, but I am not doing that here. I also added a handler for EnabledChanged event.

// Required for ownerdraw
this.DrawItem += new DrawItemEventHandler(EnableDisplayCombo_DrawItem);

// Required for ownerdraw
this.DrawMode = DrawMode.OwnerDrawFixed;
this.EnabledChanged += new EventHandler(EnableDisplayCombo_EnabledChanged);

Next, in the EnabledChanged event, I change the DropDownStyle based upon whether the control is enabled or not. A DropDownStyle of just DropDown will allow the control to be typed into, but DropDownList will only allow the use of the mouse. The catch is that you can only control the appearance of the EditBox if you set the DropDownStyle to DropDownList. For a disabled control, it doesn't matter since you can't select it anyway.

void EnableDisplayCombo_EnabledChanged(object sender, EventArgs e)
    if (this.Enabled)
        this.DropDownStyle = ComboBoxStyle.DropDown;
        this.DropDownStyle = ComboBoxStyle.DropDownList;

Finally, here is the ownerdraw routine. Basically, you just key on the state of the item that needs to be drawn, and draw it however you want.

void EnableDisplayCombo_DrawItem(object sender, DrawItemEventArgs e)
    System.Drawing.Graphics g = e.Graphics;    
    Rectangle r = e.Bounds;    
    if (e.Index >= 0)    
        string label = this.Items[e.Index].ToString();        
        // This is how we draw a disabled control        
        if (e.State == (DrawItemState.Disabled | DrawItemState.NoAccelerator 
                | DrawItemState.NoFocusRect | DrawItemState.ComboBoxEdit))   
            e.Graphics.FillRectangle(new SolidBrush(Color.White), r);      
            g.DrawString(label, e.Font, Brushes.Black, r);            
        // This is how we draw the items in an enabled control that aren't<BR>        // in focus        
        else if (e.State == (DrawItemState.NoAccelerator | <BR>                             DrawItemState.NoFocusRect))        
            e.Graphics.FillRectangle(new SolidBrush(Color.White), r); 
            g.DrawString(label, e.Font, Brushes.Black, r);            
        // This is how we draw the focused items        
            e.Graphics.FillRectangle(new SolidBrush(Color.Blue), r); 
            g.DrawString(label, e.Font, Brushes.White, r);            

Note that if you use databinding you will have to use a little more complex code in the line where the label is assigned. Maybe something like this:

string label = Convert.ToString(item[this.DisplayMember]);

I hope this is useful to somebody. The solution is pretty simple, but it took me a long time to piece together from various sources. Any comments or suggestions are very welcome.


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


About the Author

Web Developer
United States United States
Alex Kilpatrick started writing software in 1981, modifying computer games written in basic. Since then, he has dabbled off and on in about 15 different languages, but currently prefers C#.

You may also be interested in...


Comments and Discussions

SuggestionKIS Pin
Member 1255198528-May-16 8:18
memberMember 1255198528-May-16 8:18 
GeneralUnneeded Dispose Pin
Michael Ehrt24-May-11 1:54
memberMichael Ehrt24-May-11 1:54 
GeneralUnreleased resources Pin
Member 733103427-Aug-10 12:40
memberMember 733103427-Aug-10 12:40 
GeneralMissing Row Highlight Pin
Member 733103427-Aug-10 12:26
memberMember 733103427-Aug-10 12:26 
GeneralBug Update Pin
Bib3477016-Sep-09 23:10
memberBib3477016-Sep-09 23:10 
Questionif comboBox's DropDownStyle = ComboBoxStyle.Simple, It looks as regular comboBox when disabled. Pin
flyrain00022-Jul-09 17:08
memberflyrain00022-Jul-09 17:08 
GeneralVery good tutorial! Thanks very much! Pin
sti_sti13-Aug-08 0:36
membersti_sti13-Aug-08 0:36 
GeneralAwesome Pin
Rakesh B Singh2-Jun-07 1:28
memberRakesh B Singh2-Jun-07 1:28 
GeneralDatabinding revisited Pin
Johnny J.26-Apr-07 5:53
memberJohnny J.26-Apr-07 5:53 
GeneralRe: Databinding revisited Pin
annielhy20-Jun-08 17:49
memberannielhy20-Jun-08 17:49 
GeneralDropDownList Pin
Johnny J.24-Apr-07 1:16
memberJohnny J.24-Apr-07 1:16 
GeneralUpdate for WinXP and W2K3 Pin
Rine21-Mar-07 23:44
memberRine21-Mar-07 23:44 
QuestionHow to handle databound combobox Pin
sjoske13-Sep-06 4:48
membersjoske13-Sep-06 4:48 
GeneralNice, but a few comments though Pin
Henk van der Geld8-Aug-06 7:07
memberHenk van der Geld8-Aug-06 7:07 
GeneralRe: Nice, but a few comments though Pin
Abhlu10-Oct-06 17:22
memberAbhlu10-Oct-06 17:22 
GeneralASP.NET Version Pin
rjpaulsen7-Aug-06 5:49
memberrjpaulsen7-Aug-06 5:49 
GeneralRe: ASP.NET Version Pin
AlexKilpatrick7-Aug-06 6:15
memberAlexKilpatrick7-Aug-06 6:15 
GeneralExcellent Trick !! Pin
Marcos Meli7-Aug-06 4:45
memberMarcos Meli7-Aug-06 4:45 
GeneralI'll certainly have to take a look Pin
Ilíon7-Aug-06 4:18
memberIlíon7-Aug-06 4:18 
GeneralRe: I'll certainly have to take a look Pin
AlexKilpatrick7-Aug-06 6:17
memberAlexKilpatrick7-Aug-06 6:17 
GeneralRe: I'll certainly have to take a look Pin
Ilíon7-Aug-06 10:10
memberIlíon7-Aug-06 10:10 
GeneralRe: I'll certainly have to take a look Pin
Ilíon18-Aug-06 14:42
memberIlíon18-Aug-06 14:42 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 6 Aug 2006
Article Copyright 2006 by AlexKilpatrick
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid