C# 如何在 .NET 中使 ComboBox 不可编辑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/85702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:26:03  来源:igfitidea点击:

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

提问by Cory Engebretson

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

我想要一个“仅选择” ComboBox,它提供一个供用户选择的项目列表。应在ComboBox控件的文本部分禁用键入。

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

我最初对此进行了谷歌搜索,结果发现一个过于复杂、误导性的建议来捕捉KeyPress事件。

采纳答案by Cory Engebretson

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:

要使 ComboBox 的文本部分不可编辑,请将 DropDownStyle 属性设置为“DropDownList”。ComboBox 现在本质上是仅供用户选择的。您可以在 Visual Studio 设计器或 C# 中执行此操作,如下所示:

stateComboBox.DropDownStyle = ComboBoxStyle.DropDownList;

Link to the documentation for the ComboBox DropDownStyle propertyon MSDN.

链接到MSDN 上ComboBox DropDownStyle 属性的文档。

回答by LZara

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

留在您的 ComboBox 上并从属性窗口搜索 DropDropStyle 属性,然后选择DropDownList

回答by invertigo

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

要添加 Visual Studio GUI 参考,您可以DropDownStyle在所选组合框的属性下找到选项:

enter image description here

在此处输入图片说明

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

这将自动将第一个答案中提到的行添加到 Form.Designer.cs InitializeComponent(),如下所示:

this.comboBoxBatch.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

回答by Abhishek Jaiswal

COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;

COMBOBOXID.DropDownStyle = ComboBoxStyle.DropDownList;

回答by Diogo Rodrigues

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