28

I'm writing a GUI in C#, Visual Studio 2008, using the Designer and WinForms. I've got a ComboBox control, and I'd like it to only allow to select from the provided options and not to accept a user-entered string. It doesn't appear to have a ReadOnly property, and disabling it hinders the readability of the control (as well as disallowing user-selection).

Brock Greman
  • 427
  • 1
  • 7
  • 10
  • The DropDownStyle property was what I was looking for. Can't believe I overlooked that. – Brock Greman Oct 02 '08 at 15:31
  • 3
    possible duplicate of [How can I make a ComboBox non-editable in .net?](http://stackoverflow.com/questions/85702/how-can-i-make-a-combobox-non-editable-in-net) – nawfal Dec 29 '13 at 02:15

5 Answers5

71

Set DropDownStyle to "DropDownList"

Phillip Wells
  • 7,402
  • 9
  • 43
  • 41
11

Set the ComboBox.DropDownStyle property to ComboBoxStyle.DropDownList.

OregonGhost
  • 23,359
  • 7
  • 71
  • 108
4

Use code similar to the following to set the allowed options and only those options.

comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.AddRange(new object[] {
    "One",
    "Two",
    "Three",
    "Four"});
Mike G
  • 4,232
  • 9
  • 40
  • 66
David Max
  • 1,035
  • 5
  • 13
  • 16
3

Another simple way to go about it.

private void combobox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
}
Isuru
  • 30,617
  • 60
  • 187
  • 303
0

Try using a DropDownListbox

Mauro
  • 4,531
  • 3
  • 30
  • 56