Click here to Skip to main content
13,797,648 members
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 8 Mar 2004

Read only ComboBox

, 11 Oct 2004
Rate this:
Please Sign up or sign in to vote.
How to make ComboBox read only


Surprisingly the MS Visual Studio ComboBox doesn’t have such an important property: ReadOnly. After searching on internet for solving this problem and not finding a relevant resolution I decided to add this important property to a new ComboBox control.

Using the code

Create a new derived class from ComboBox:

public class NewComboBox    : System.Windows.Forms.ComboBox

Declare private variables:

private bool readOnly;
private Color tBackColor;
private Color tForeColor;
private ContextMenu cmnuEmpty;
private ComboBoxStyle dropDownStyle = ComboBoxStyle.DropDown;
private Color readOnlyBackColor = SystemColors.Control;
private Color readOnlyForeColor = SystemColors.WindowText;

Set custom context menu to be display when read only:

public ReadOnlyComboBox()
  cmnuEmpty = new ContextMenu(); //code this menu for your needs

Declare ReadOnly property:

public bool ReadOnly
    return readOnly;
    if(readOnly != value)
      readOnly = value;
        this.ContextMenu = cmnuEmpty;
        base.DropDownStyle = ComboBoxStyle.Simple;
        base.Height = 21;
        this.tBackColor = this.BackColor;
        this.tForeColor = this.ForeColor;
        base.BackColor = this.ReadOnlyBackColor;
        base.ForeColor = this.ReadOnlyForeColor;
        this.ContextMenu = null;
        base.DropDownStyle = dropDownStyle;
        base.BackColor = tBackColor;
        base.ForeColor = tForeColor;

Override OnKeyDown and OnKeyPress events to stop editing when readonly:

protected override void OnKeyDown(KeyEventArgs e)  
  if(this.ReadOnly && (
    e.KeyCode == Keys.Up  || 
    e.KeyCode == Keys.Down  ||
    e.KeyCode == Keys.Delete))
    e.Handled = true;
    base.OnKeyDown (e);

protected override void OnKeyPress(KeyPressEventArgs e) 
        e.Handled = true;
        base.OnKeyPress (e);

Change the DropDownStyle property to reflect property changes only when not ReadOnly:

new public ComboBoxStyle DropDownStyle
    return dropDownStyle;
    if(dropDownStyle != value)
      dropDownStyle = value;
      if(!this.ReadOnly) base.DropDownStyle = value;

Add back/fore color properties to reflect changes in ReadOnly property:

public Color ReadOnlyBackColor
    return readOnlyBackColor;
    readOnlyBackColor = value;
public Color ReadOnlyForeColor
    return readOnlyForeColor;
    readOnlyForeColor = value;


Eliminate the white border that appears inside the client area when enable XP styles and BackColor is not default color.


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

Software Developer (Senior)
Romania Romania
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMust also disable mouse paste Pin
FinishedOnTime26-Feb-07 4:25
memberFinishedOnTime26-Feb-07 4:25 
QuestionLatest code? Pin
Chuck_Esterbrook27-Dec-06 14:43
memberChuck_Esterbrook27-Dec-06 14:43 
GeneralKeyPress Event Handler Pin
jsteve1712-Dec-06 10:16
memberjsteve1712-Dec-06 10:16 
GeneralVery good. Pin
jotaele31-Oct-06 0:00
memberjotaele31-Oct-06 0:00 
QuestionHow to remove the white border? Pin
sick bastard1-Jan-06 22:59
membersick bastard1-Jan-06 22:59 
GeneralAnother approach for a ReadOnly ComboBox Pin
Claudio Grazioli9-May-05 11:12
memberClaudio Grazioli9-May-05 11:12 
GeneralRe: Another approach for a ReadOnly ComboBox Pin
Chuck_Esterbrook27-Dec-06 13:35
memberChuck_Esterbrook27-Dec-06 13:35 
GeneralToo bad.... Pin
emunews28-Feb-05 3:48
memberemunews28-Feb-05 3:48 
GeneralReadOnlyComboBox in DropDownList Mode Pin
pgolds6-Dec-04 6:21
memberpgolds6-Dec-04 6:21 
GeneralRe: ReadOnlyComboBox in DropDownList Mode Pin
pgolds6-Dec-04 8:22
memberpgolds6-Dec-04 8:22 
GeneralRe: ReadOnlyComboBox in DropDownList Mode Pin
Dan.A.6-Dec-04 20:30
memberDan.A.6-Dec-04 20:30 
GeneralRe: ReadOnlyComboBox in DropDownList Mode Pin
pgolds7-Dec-04 5:34
memberpgolds7-Dec-04 5:34 
GeneralOptional solution Pin
omacias20-Nov-04 9:43
memberomacias20-Nov-04 9:43 
GeneralRe: Optional solution Pin
Anonymous21-Nov-04 1:02
memberAnonymous21-Nov-04 1:02 
GeneralThank You. Pin
pgolds19-Nov-04 10:33
memberpgolds19-Nov-04 10:33 
GeneralRefreshing a DropDownList Pin
Paco7612-Nov-04 8:31
memberPaco7612-Nov-04 8:31 
GeneralNice. One little remark. Pin
Boyan Botev19-Oct-04 22:14
memberBoyan Botev19-Oct-04 22:14 
GeneralRMB option cut enabled Pin
Member 4985535-Oct-04 1:03
memberMember 4985535-Oct-04 1:03 
GeneralRe: RMB option cut enabled Pin
Dan.A.5-Oct-04 20:22
memberDan.A.5-Oct-04 20:22 
GeneralRe: RMB option cut enabled Pin
Member 49855325-Oct-04 0:56
memberMember 49855325-Oct-04 0:56 
GeneralRe: Re: RMB option cut enabled Pin
Member 49855325-Oct-04 0:58
memberMember 49855325-Oct-04 0:58 
QuestionWhy not just change the DropDownStyle Property? Pin
Anonymous30-May-04 8:11
memberAnonymous30-May-04 8:11 
AnswerRe: Why not just change the DropDownStyle Property? Pin
Dan.A.30-May-04 19:57
memberDan.A.30-May-04 19:57 
GeneralDropDownList can open, but selection can't be changed. Pin
Hanan Nachmany25-Apr-04 2:35
memberHanan Nachmany25-Apr-04 2:35 
GeneralCombobox still recieves F4, PageDown and pageUp Pin
Guemper17-Mar-04 21:11
memberGuemper17-Mar-04 21:11 

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 | Cookies | Terms of Use | Mobile
Web05 | 2.8.181207.3 | Last Updated 11 Oct 2004
Article Copyright 2004 by Dan.A.
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid