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

ReadOnly Combobox

Rate me:
Please Sign up or sign in to vote.
1.86/5 (5 votes)
7 May 20071 min read 59.4K   375   14   7
Creating simple, attractive, read-only comboboxes

Introduction

Just like a lot of people, I was looking for a read-only combobox. Most of the code samples available are complex, have some faults or just aren't that kind of read-only we want. So I went looking for a simple solution using Windows messages to keep the drop-down closed. As you will see in this article, the code is pretty easy.

Using the code

Inherit a class from a combobox and override the events that can modify the value of it. Under events, you can check if the combobox is read-only, as well as block or ignore the input.

// =============================================
// Author: KC
// Create date: 03/05/07
// Description: ReadOonly ComboBox
// =============================================
    class RoComboBox : System.Windows.Forms.ComboBox
    {
        private bool readOnly;
        public bool ReadOnly
        {
            get { return readOnly;}
            set { readOnly = value;}
        }
        protected override void  
            OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (readOnly)
                e.Handled = true;
             base.OnKeyDown(e);
        }
        protected override void 
            OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            if (readOnly)
                e.Handled = true;
            base.OnKeyPress(e);
        }
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            // WM_LBUTTONDOWN && WM_LBUTTONDBLCLK
            if ((m.Msg != 0x201 && m.Msg != 0x203) || !readOnly)
                base.WndProc(ref m);
        }
    }

So, what is this code doing?

It is adding a private field and a property called ReadOnly.

private bool readOnly;
       public bool ReadOnly
       {
           get { return readOnly;}
           set { readOnly = value;}
       }
Override KeyDown and KeyPress; check if the property ReadOnly is true and set as Handled. Then KeyPress will not modify the combobox value.
protected override void
    OnKeyDown(System.Windows.Forms.KeyEventArgs e)
{
    if (readOnly)
        e.Handled = true;
     base.OnKeyDown(e);
}

protected override void
    OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
    if (readOnly)
        e.Handled = true;
    base.OnKeyPress(e);
}
The last thing you have to do is block the mouse to prevent the drop-down from opening. You can do this by overriding the WndProc of the combobox and checking the type of Windows message. Block the mouse buttons when the property ReadOnly is set.
protected override void WndProc(ref System.Windows.Forms.Message m)
{
    // WM_LBUTTONDOWN && WM_LBUTTONDBLCLK
    if ((m.Msg != 0x201 && m.Msg != 0x203) || !readOnly)
        base.WndProc(ref m);
}

Points of interest

If you want to know more about Windows messages or look for some constant values for them, check out http://www.pinvoke.net/default.aspx/Constants.WM

History

Original version posted: May 7, 2007

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)
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMinor sugestion Pin
PederA7-Jul-09 22:51
PederA7-Jul-09 22:51 
Usefull code!
I have a minor sugestion to enable key up/down:

Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)<br />
        If _Readonly And e.KeyCode <> Keys.Up And e.KeyCode <> Keys.Down Then<br />
            e.Handled = True<br />
        End If<br />
        MyBase.OnKeyDown(e)<br />
End Sub


/Peder
GeneralThank you that is awesome!! I added it translated into VB incase that helps anyone. Pin
seansos1-Jul-09 6:51
seansos1-Jul-09 6:51 
GeneralRe: Thank you that is awesome!! I added it translated into VB incase that helps anyone. Pin
matzone8-Nov-14 21:40
matzone8-Nov-14 21:40 
Generalno code... Pin
Niiiissssshhhhhuuuuu7-May-07 18:30
Niiiissssshhhhhuuuuu7-May-07 18:30 
GeneralRe: no code... Pin
topcatalpha7-May-07 20:30
topcatalpha7-May-07 20:30 
GeneralSource code missing Pin
Tony Bermudez7-May-07 18:15
Tony Bermudez7-May-07 18:15 
GeneralRe: Source code missing Pin
topcatalpha10-May-07 20:33
topcatalpha10-May-07 20:33 

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.