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

C#.NetWinformsCombobox

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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;

Solution 3 - C#

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

Solution 4 - C#

COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;

Solution 5 - C#

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;
}

Solution 6 - C#

Before

enter image description here

Method1

enter image description here

Method2

cmb_type.DropDownStyle=ComboBoxStyle.DropDownList

After

enter image description here

Solution 7 - C#

for winforms .NET change DropDownStyle to DropDownList from Combobox property

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCory EngebretsonView Question on Stackoverflow
Solution 1 - C#Cory EngebretsonView Answer on Stackoverflow
Solution 2 - C#invertigoView Answer on Stackoverflow
Solution 3 - C#LZaraView Answer on Stackoverflow
Solution 4 - C#Abhishek JaiswalView Answer on Stackoverflow
Solution 5 - C#Diogo RodriguesView Answer on Stackoverflow
Solution 6 - C#lavaView Answer on Stackoverflow
Solution 7 - C#Rami RoosanView Answer on Stackoverflow