Click here to Skip to main content
Click here to Skip to main content

Read only ComboBox

By , 11 Oct 2004
 

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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMust also disable mouse pastememberFinishedOnTime26-Feb-07 3:25 
QuestionLatest code?memberChuck_Esterbrook27-Dec-06 13:43 
GeneralKeyPress Event Handlermemberjsteve1712-Dec-06 9:16 
GeneralVery good.memberjotaele30-Oct-06 23:00 
QuestionHow to remove the white border?membersick bastard1-Jan-06 21:59 
GeneralAnother approach for a ReadOnly ComboBoxmemberClaudio Grazioli9-May-05 10:12 
GeneralRe: Another approach for a ReadOnly ComboBoxmemberChuck_Esterbrook27-Dec-06 12:35 
GeneralToo bad....memberemunews28-Feb-05 2:48 
GeneralReadOnlyComboBox in DropDownList Modememberpgolds6-Dec-04 5:21 
GeneralRe: ReadOnlyComboBox in DropDownList Modememberpgolds6-Dec-04 7:22 
GeneralRe: ReadOnlyComboBox in DropDownList ModememberDan Anatoli6-Dec-04 19:30 
GeneralRe: ReadOnlyComboBox in DropDownList Modememberpgolds7-Dec-04 4:34 
GeneralOptional solutionmemberomacias20-Nov-04 8:43 
GeneralRe: Optional solutionsussAnonymous21-Nov-04 0:02 
GeneralThank You.memberpgolds19-Nov-04 9:33 
GeneralRefreshing a DropDownListmemberPaco7612-Nov-04 7:31 
GeneralNice. One little remark.memberBoyan Botev19-Oct-04 21:14 
GeneralRMB option cut enabledmemberasela.gunawardena@ifs.lk5-Oct-04 0:03 
GeneralRe: RMB option cut enabledmemberDan Anatoli5-Oct-04 19:22 
GeneralRe: RMB option cut enabledmemberAsela Gunawardena24-Oct-04 23:56 
GeneralRe: Re: RMB option cut enabledmemberAsela Gunawardena24-Oct-04 23:58 
QuestionWhy not just change the DropDownStyle Property?sussAnonymous30-May-04 7:11 
AnswerRe: Why not just change the DropDownStyle Property?memberDan Anatoli30-May-04 18:57 
GeneralDropDownList can open, but selection can't be changed.memberHanan Nachmany25-Apr-04 1:35 
GeneralCombobox still recieves F4, PageDown and pageUpmemberGuemper17-Mar-04 20:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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