Click here to Skip to main content
Licence 
First Posted 8 Mar 2004
Views 176,094
Bookmarked 33 times

Read only ComboBox

By | 11 Oct 2004 | Article
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

About the Author

Dan.A.

Software Developer (Senior)

Romania Romania

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMust also disable mouse paste PinmemberFinishedOnTime3:25 26 Feb '07  
QuestionLatest code? PinmemberChuck_Esterbrook13:43 27 Dec '06  
GeneralKeyPress Event Handler Pinmemberjsteve179:16 12 Dec '06  
GeneralVery good. Pinmemberjotaele23:00 30 Oct '06  
QuestionHow to remove the white border? Pinmembersick bastard21:59 1 Jan '06  
GeneralAnother approach for a ReadOnly ComboBox PinmemberClaudio Grazioli10:12 9 May '05  
GeneralRe: Another approach for a ReadOnly ComboBox PinmemberChuck_Esterbrook12:35 27 Dec '06  
GeneralToo bad.... Pinmemberemunews2:48 28 Feb '05  
GeneralReadOnlyComboBox in DropDownList Mode Pinmemberpgolds5:21 6 Dec '04  
GeneralRe: ReadOnlyComboBox in DropDownList Mode Pinmemberpgolds7:22 6 Dec '04  
GeneralRe: ReadOnlyComboBox in DropDownList Mode PinmemberDan Anatoli19:30 6 Dec '04  
GeneralRe: ReadOnlyComboBox in DropDownList Mode Pinmemberpgolds4:34 7 Dec '04  
GeneralOptional solution Pinmemberomacias8:43 20 Nov '04  
GeneralRe: Optional solution PinsussAnonymous0:02 21 Nov '04  
GeneralThank You. Pinmemberpgolds9:33 19 Nov '04  
GeneralRefreshing a DropDownList PinmemberPaco767:31 12 Nov '04  
GeneralNice. One little remark. PinmemberBoyan Botev21:14 19 Oct '04  
GeneralRMB option cut enabled Pinmemberasela.gunawardena@ifs.lk0:03 5 Oct '04  
GeneralRe: RMB option cut enabled PinmemberDan Anatoli19:22 5 Oct '04  
GeneralRe: RMB option cut enabled PinmemberAsela Gunawardena23:56 24 Oct '04  
GeneralRe: Re: RMB option cut enabled PinmemberAsela Gunawardena23:58 24 Oct '04  
QuestionWhy not just change the DropDownStyle Property? PinsussAnonymous7:11 30 May '04  
AnswerRe: Why not just change the DropDownStyle Property? PinmemberDan Anatoli18:57 30 May '04  
GeneralDropDownList can open, but selection can't be changed. PinmemberHanan Nachmany1:35 25 Apr '04  
GeneralCombobox still recieves F4, PageDown and pageUp PinmemberGuemper20:11 17 Mar '04  

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.

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