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

2019-01-06 12:17发布

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.

5条回答
Rolldiameter
2楼-- · 2019-01-06 12:44

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

查看更多
【Aperson】
3楼-- · 2019-01-06 12:48

COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;

查看更多
狗以群分
4楼-- · 2019-01-06 12:49

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.

查看更多
Animai°情兽
5楼-- · 2019-01-06 12:49

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;
查看更多
迷人小祖宗
6楼-- · 2019-01-06 12:50

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;
}
查看更多
登录 后发表回答