55

I am facing a problem in setting the combo property such that only user can select the values form given items, but I cannot write in the combo box.

How can I do so in C#?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Badr
  • 10,384
  • 15
  • 70
  • 104
  • This question has been asked before. Please see here : http://stackoverflow.com/questions/85702/how-can-i-make-a-combobox-non-editable-in-net – Peter Jun 17 '10 at 11:05

6 Answers6

134

Just change the DropDownStyle to DropDownList. Or if you want it completely read only you can set Enabled = false, or if you don't like the look of that I sometimes have two controls, one readonly textbox and one combobox and then hide the combo and show the textbox if it should be completely readonly and vice versa.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • 2
    But please note: readonly != disabled. – H H Jun 17 '10 at 11:09
  • 1
    @Henk: Yep, that's why I said "if you don't like the look of that..." since that's usually the main issue people find with disabling a control rather than setting it as readonly. – Hans Olsson Jun 17 '10 at 11:17
  • 1
    thanx buddy it was simple but if you don't know about this it is a mystery for you, thanx it works – Badr Jun 17 '10 at 11:20
  • 4
    too bad the combobox cannot show default text and have a dropdownlist style at the same time. – nurettin Mar 12 '17 at 17:50
21

I think you want to change the setting called "DropDownStyle" to be "DropDownList".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ryan
  • 6,756
  • 13
  • 49
  • 68
15

In the keypress event handler:

e.Handled = true;
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Naa3er
  • 167
  • 1
  • 2
9

Make the DropDownStyle to DropDownList

stateComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
Tijo Tom
  • 487
  • 6
  • 13
7

Try this:

    private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        // comboBox1 is readonly
        e.SuppressKeyPress = true;
    }
Frans
  • 81
  • 1
  • 1
  • The only solution that allows for an initial text but still does not allow the user to change the text in the `ComboBox`. Many thanks! – MarkusAnd Oct 26 '20 at 12:02
4

The solution is to change the DropDownStyle property to DropDownList. It will help.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
scatterbraiin
  • 157
  • 1
  • 2
  • 6