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.
To add a Visual Studio GUI reference, you can find the DropDownStyle
options under the Properties of the selected ComboBox:
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;
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;
}
for winforms .NET change DropDownStyle to DropDownList from Combobox property
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;
}
Stay on your ComboBox and search the DropDropStyle property from the properties window and then choose DropDownList.
COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;
for winforms .NET change DropDownStyle to DropDownList from Combobox property
COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;
Stay on your ComboBox and search the DropDropStyle property from the properties window and then choose DropDownList.
Source: Stackoverflow.com