Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a ComboBox non-editable in .NET?

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.

like image 855
Cory Engebretson Avatar asked Sep 17 '08 17:09

Cory Engebretson


2 Answers

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.

like image 126
Cory Engebretson Avatar answered Sep 28 '22 00:09

Cory Engebretson


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; 
like image 38
invertigo Avatar answered Sep 28 '22 00:09

invertigo