228

I want to have a "select-only" ComboBox that provides a list of items for the user to select from. Typing should be disabled in the text portion of the ComboBox control.

My initial googling of this turned up an overly complex, misguided suggestion to capture the KeyPress event.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cory Engebretson
  • 7,653
  • 4
  • 22
  • 17

7 Answers7

424

To make the text portion of a ComboBox non-editable, set the DropDownStyle property to "DropDownList". The ComboBox is now essentially select-only for the user. You can do this in the Visual Studio designer, or in C# like this:

stateComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

Link to the documentation for the ComboBox DropDownStyle property on MSDN.

Omar
  • 16,329
  • 10
  • 48
  • 66
Cory Engebretson
  • 7,653
  • 4
  • 22
  • 17
  • 8
    One thing to bear in mind doing this means you can no longer update the `Text` property programmically, was using it to show a default message using `ComboBox.SelectedIndex = -1;` – user692942 Nov 20 '12 at 15:55
  • Is there a way to do this through the GUI for Visual C# without having to edit it in the code? Seems like a 'default value' would be a normal thing to want set up – muzzlator May 06 '13 at 00:13
  • 7
    How do I avoid the gray appearance? – Anders Lindén May 23 '16 at 12:31
  • 3
    @AndersLindén set the property FlatStyle to Flat. – Xam Apr 12 '19 at 05:54
  • @Xam Then where does the border go? When googling a bit, it seems I have to do the drawing myself to have it look acceptable. – Anders Lindén Jul 22 '19 at 22:46
  • @AndersLindén yeah, if you don't like the default look of flat style, then the only solution is to owner draw the ComboBox. – Xam Jul 22 '19 at 23:07
  • @Xam Do you know of some alternative component library that will not require owner drawing to look great? – Anders Lindén Jul 22 '19 at 23:20
  • @AndersLindén you could try Devexpress or Telerik. Another alternative is to keep the `FlatStyle` to `Standard` and use this [answer](https://stackoverflow.com/a/3789751/8781554) to change the default gray appareance of the `DropDownList` style. – Xam Jul 22 '19 at 23:38
  • Thanks for the suggestion, but I think you should never need to draw in a component that should be ready to use as it is. – Anders Lindén Jul 23 '19 at 00:30
76

To add a Visual Studio GUI reference, you can find the DropDownStyle options under the Properties of the selected ComboBox:

enter image description here

Which will automatically add the line mentioned in the first answer to the Form.Designer.cs InitializeComponent(), like so:

this.comboBoxBatch.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
invertigo
  • 6,336
  • 5
  • 39
  • 64
33

Stay on your ComboBox and search the DropDropStyle property from the properties window and then choose DropDownList.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LZara
  • 385
  • 3
  • 5
4

Before

enter image description here

Method1

enter image description here

Method2

cmb_type.DropDownStyle=ComboBoxStyle.DropDownList

After

enter image description here

lava
  • 6,020
  • 2
  • 31
  • 28
2
COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;
dimitar.bogdanov
  • 387
  • 2
  • 10
Abhishek Jaiswal
  • 1,161
  • 12
  • 6
1

To continue displaying data in the input after selecting, do so:

VB.NET
Private Sub ComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ComboBox1.KeyPress
    e.Handled = True
End Sub



C#
Private void ComboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
}
Diogo Rodrigues
  • 1,312
  • 15
  • 15
0

for winforms .NET change DropDownStyle to DropDownList from Combobox property