65.9K
CodeProject is changing. Read more.
Home

ReadOnly Combobox

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.86/5 (5 votes)

May 7, 2007

1 min read

viewsIcon

60243

downloadIcon

375

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