C# WinForms 中的只读组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/162936/
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 16:19:11  来源:igfitidea点击:

Readonly ComboBox in WinForms

c#winformscomboboxreadonly

提问by Brock Greman

I'm writing a GUI in C#, Visual Studio 2008, using the Designer and WinForms. I've got a ComboBox control, and I'd like it to only allow to select from the provided options and not to accept a user-entered string. It doesn't appear to have a ReadOnly property, and disabling it hinders the readability of the control (as well as disallowing user-selection).

我正在使用设计器和 WinForms 在 C#、Visual Studio 2008 中编写 GUI。我有一个 ComboBox 控件,我希望它只允许从提供的选项中进行选择,而不接受用户输入的字符串。它似乎没有 ReadOnly 属性,禁用它会妨碍控件的可读性(以及禁止用户选择)。

采纳答案by Phillip Wells

Set DropDownStyle to "DropDownList"

将 DropDownStyle 设置为“DropDownList”

回答by Mauro

Try using a DropDownListbox

尝试使用 DropDownListbox

回答by OregonGhost

Set the ComboBox.DropDownStyle property to ComboBoxStyle.DropDownList.

将 ComboBox.DropDownStyle 属性设置为 ComboBoxStyle.DropDownList。

回答by David Max

Use code similar to the following to set the allowed options and only those options.

使用类似于以下的代码来设置允许的选项,并且仅设置那些选项。

comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.AddRange(new object[] {
    "One",
    "Two",
    "Three",
    "Four"});

回答by Isuru

Another simple way to go about it.

另一种简单的方法来解决它。

private void combobox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = true;
}