Click here to Skip to main content
15,887,214 members
Articles / Programming Languages / C#
Article

Read only ComboBox

Rate me:
Please Sign up or sign in to vote.
3.37/5 (26 votes)
11 Oct 2004 234.3K   1.7K   33   44
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:

C#
public class NewComboBox    : System.Windows.Forms.ComboBox

Declare private variables:

C#
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:

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

Declare ReadOnly property:

C#
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:

C#
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:

C#
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:

C#
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


Written By
Software Developer (Senior)
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Combobox still recieves F4, PageDown and pageUp Pin
Dan.A.18-Mar-04 18:23
Dan.A.18-Mar-04 18:23 
GeneralRe: Combobox still recieves F4, PageDown and pageUp Pin
edokan3-May-04 6:20
edokan3-May-04 6:20 
General...and you can cut the text Pin
Marcuse726-May-04 5:14
Marcuse726-May-04 5:14 
GeneralRe: ...and you can cut the text Pin
Dan.A.30-May-04 23:02
Dan.A.30-May-04 23:02 
GeneralThat's the hard way... Pin
Ray Hayes12-Mar-04 4:02
Ray Hayes12-Mar-04 4:02 
GeneralRe: That's the hard way... Pin
Dan.A.14-Mar-04 18:50
Dan.A.14-Mar-04 18:50 
GeneralThe easiest way.. Pin
yltsa25-Mar-05 21:28
yltsa25-Mar-05 21:28 
GeneralRe: That's the hard way... Pin
System.Object21-Apr-06 9:29
System.Object21-Apr-06 9:29 
QuestionHOW to add this NewComboBox to the form? Pin
Nappo2310-Mar-04 8:21
Nappo2310-Mar-04 8:21 
AnswerRe: HOW to add this NewComboBox to the form? Pin
Dan.A.10-Mar-04 19:48
Dan.A.10-Mar-04 19:48 
GeneralCombox the worst control Pin
<marquee>ElNoNo10-Mar-04 3:44
<marquee>ElNoNo10-Mar-04 3:44 
GeneralRe: Combox the worst control Pin
Roger Alsing10-Mar-04 3:52
Roger Alsing10-Mar-04 3:52 
GeneralRe: Combox the worst control Pin
SebCode11-Oct-04 3:44
SebCode11-Oct-04 3:44 
QuestionDropDownList? Pin
Uwe Keim9-Mar-04 6:31
sitebuilderUwe Keim9-Mar-04 6:31 
AnswerRe: DropDownList? Pin
Dan.A.9-Mar-04 18:50
Dan.A.9-Mar-04 18:50 
GeneralRe: DropDownList? Pin
stringali6911-Mar-04 5:13
stringali6911-Mar-04 5:13 
GeneralRe: DropDownList? Pin
Dan.A.11-Mar-04 18:46
Dan.A.11-Mar-04 18:46 
GeneralRe: DropDownList? Pin
Anonymous15-Aug-04 21:09
Anonymous15-Aug-04 21:09 
GeneralRe: DropDownList? Pin
Dan.A.16-Aug-04 0:52
Dan.A.16-Aug-04 0:52 

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.