C# 是否可以更改 Winforms 组合框以禁用输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2367742/
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
Is it possible to change a Winforms combobox to disable typing into it?
提问by Joan Venge
So that it just allows selecting items already inside, but not allow typing/editing the text inside it?
所以它只允许选择已经在里面的项目,但不允许在里面输入/编辑文本?
采纳答案by Shane Fulmer
Set ComboBox.DropDownStyle
to ComboBoxStyle.DropDownList
.
设置ComboBox.DropDownStyle
为ComboBoxStyle.DropDownList
。
回答by teynon
After trying ShaneFulmer's answer, I noticed that the style of the drop down was changed. This was a problem for me and apparently there is no good way of changing it. (Background color doesn't actually change it.)
在尝试了 ShaneFulmer 的回答后,我注意到下拉列表的样式发生了变化。这对我来说是一个问题,显然没有改变它的好方法。(背景颜色实际上并没有改变它。)
I ended up adding a keypress handler to prevent adding text.
我最终添加了一个按键处理程序来防止添加文本。
private void myCombo_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}