Click here to Skip to main content
11,634,568 members (68,587 online)
Click here to Skip to main content

Read only ComboBox

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

Introduction

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
{
  get 
  {
    return readOnly;
  }
  set
  {
    if(readOnly != value)
    {
      readOnly = value;
      if(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;
      }
      else
      {
        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;
  else
    base.OnKeyDown (e);
}

protected override void OnKeyPress(KeyPressEventArgs e) 
{
    if(this.ReadOnly) 
        e.Handled = true;
    else
        base.OnKeyPress (e);
}

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

new public ComboBoxStyle DropDownStyle
{
  get
  {
    return dropDownStyle;
  }
  set
  {
    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
{
  get
  {
    return readOnlyBackColor;
  }
  set
  {
    readOnlyBackColor = value;
  }
}
    
public Color ReadOnlyForeColor
{
  get
  {
    return readOnlyForeColor;
  }
  set
  {
    readOnlyForeColor = value;
  }
}

ToDo

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

License

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

Share

About the Author

Dan.A.
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 3:25
memberFinishedOnTime26-Feb-07 3:25 
QuestionLatest code? Pin
Chuck_Esterbrook27-Dec-06 13:43
memberChuck_Esterbrook27-Dec-06 13:43 
GeneralKeyPress Event Handler Pin
jsteve1712-Dec-06 9:16
memberjsteve1712-Dec-06 9:16 
GeneralVery good. Pin
jotaele30-Oct-06 23:00
memberjotaele30-Oct-06 23:00 
QuestionHow to remove the white border? Pin
sick bastard1-Jan-06 21:59
membersick bastard1-Jan-06 21:59 
GeneralAnother approach for a ReadOnly ComboBox Pin
Claudio Grazioli9-May-05 10:12
memberClaudio Grazioli9-May-05 10:12 
GeneralRe: Another approach for a ReadOnly ComboBox Pin
Chuck_Esterbrook27-Dec-06 12:35
memberChuck_Esterbrook27-Dec-06 12:35 
GeneralToo bad.... Pin
emunews28-Feb-05 2:48
memberemunews28-Feb-05 2:48 
GeneralReadOnlyComboBox in DropDownList Mode Pin
pgolds6-Dec-04 5:21
memberpgolds6-Dec-04 5:21 
I seem to have discovered an issue. Hoping you can help me solve it. In the native ComboBox control, when it is set for 'DropDownList' style and you press a key, it jumps to the first entry with that letter (and then the second, third, fourth, etc.. entry as you keep pressing the same button). However, with this Read Only ComboBox, I seem to be able to type data into the edit part. Is there a way to regain the Native Combobox DropDownList functionality while retaining the Read-Only-ability of this control?

Thanks.
GeneralRe: ReadOnlyComboBox in DropDownList Mode Pin
pgolds6-Dec-04 7:22
memberpgolds6-Dec-04 7:22 
GeneralRe: ReadOnlyComboBox in DropDownList Mode Pin
Dan Anatoli6-Dec-04 19:30
memberDan Anatoli6-Dec-04 19:30 
GeneralRe: ReadOnlyComboBox in DropDownList Mode Pin
pgolds7-Dec-04 4:34
memberpgolds7-Dec-04 4:34 
GeneralOptional solution Pin
omacias20-Nov-04 8:43
memberomacias20-Nov-04 8:43 
GeneralRe: Optional solution Pin
Anonymous21-Nov-04 0:02
sussAnonymous21-Nov-04 0:02 
GeneralThank You. Pin
pgolds19-Nov-04 9:33
memberpgolds19-Nov-04 9:33 
GeneralRefreshing a DropDownList Pin
Paco7612-Nov-04 7:31
memberPaco7612-Nov-04 7:31 
GeneralNice. One little remark. Pin
Boyan Botev19-Oct-04 21:14
memberBoyan Botev19-Oct-04 21:14 
GeneralRMB option cut enabled Pin
asela.gunawardena@ifs.lk5-Oct-04 0:03
memberasela.gunawardena@ifs.lk5-Oct-04 0:03 
GeneralRe: RMB option cut enabled Pin
Dan Anatoli5-Oct-04 19:22
memberDan Anatoli5-Oct-04 19:22 
GeneralRe: RMB option cut enabled Pin
Asela Gunawardena24-Oct-04 23:56
memberAsela Gunawardena24-Oct-04 23:56 
GeneralRe: Re: RMB option cut enabled Pin
Asela Gunawardena24-Oct-04 23:58
memberAsela Gunawardena24-Oct-04 23:58 
QuestionWhy not just change the DropDownStyle Property? Pin
Anonymous30-May-04 7:11
sussAnonymous30-May-04 7:11 
AnswerRe: Why not just change the DropDownStyle Property? Pin
Dan Anatoli30-May-04 18:57
memberDan Anatoli30-May-04 18:57 
GeneralDropDownList can open, but selection can't be changed. Pin
Hanan Nachmany25-Apr-04 1:35
memberHanan Nachmany25-Apr-04 1:35 
GeneralCombobox still recieves F4, PageDown and pageUp Pin
Guemper17-Mar-04 20:11
memberGuemper17-Mar-04 20:11 
GeneralRe: Combobox still recieves F4, PageDown and pageUp Pin
Dan Anatoli18-Mar-04 18:23
memberDan Anatoli18-Mar-04 18:23 
GeneralRe: Combobox still recieves F4, PageDown and pageUp Pin
edokan3-May-04 6:20
memberedokan3-May-04 6:20 
General...and you can cut the text Pin
Marcuse726-May-04 5:14
memberMarcuse726-May-04 5:14 
GeneralRe: ...and you can cut the text Pin
Dan Anatoli30-May-04 23:02
memberDan Anatoli30-May-04 23:02 
GeneralThat's the hard way... Pin
Ray Hayes12-Mar-04 4:02
memberRay Hayes12-Mar-04 4:02 
GeneralRe: That's the hard way... Pin
Dan Anatoli14-Mar-04 18:50
memberDan Anatoli14-Mar-04 18:50 
GeneralThe easiest way.. Pin
yltsa25-Mar-05 21:28
memberyltsa25-Mar-05 21:28 
GeneralRe: That's the hard way... Pin
System.Object21-Apr-06 9:29
memberSystem.Object21-Apr-06 9:29 
QuestionHOW to add this NewComboBox to the form? Pin
Nappo2310-Mar-04 8:21
memberNappo2310-Mar-04 8:21 
AnswerRe: HOW to add this NewComboBox to the form? Pin
Dan Anatoli10-Mar-04 19:48
memberDan Anatoli10-Mar-04 19:48 
GeneralCombox the worst control Pin
ElNoNo10-Mar-04 3:44
memberElNoNo10-Mar-04 3:44 
GeneralRe: Combox the worst control Pin
Roger J10-Mar-04 3:52
memberRoger J10-Mar-04 3:52 
GeneralRe: Combox the worst control Pin
SebCode11-Oct-04 3:44
memberSebCode11-Oct-04 3:44 
QuestionDropDownList? Pin
Uwe Keim9-Mar-04 6:31
sitebuilderUwe Keim9-Mar-04 6:31 
AnswerRe: DropDownList? Pin
Dan Anatoli9-Mar-04 18:50
memberDan Anatoli9-Mar-04 18:50 
GeneralRe: DropDownList? Pin
stringali6911-Mar-04 5:13
memberstringali6911-Mar-04 5:13 
GeneralRe: DropDownList? Pin
Dan Anatoli11-Mar-04 18:46
memberDan Anatoli11-Mar-04 18:46 
GeneralRe: DropDownList? Pin
Anonymous15-Aug-04 21:09
sussAnonymous15-Aug-04 21:09 
GeneralRe: DropDownList? Pin
Dan Anatoli16-Aug-04 0:52
memberDan Anatoli16-Aug-04 0:52 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150728.1 | Last Updated 11 Oct 2004
Article Copyright 2004 by Dan.A.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid