如何在 .NET 中使 ComboBox 不可编辑? [英] How can I make a ComboBox non-editable in .NET?
问题描述
我想要一个仅选择"ComboBox
,它为用户提供一个可供选择的项目列表.应在 ComboBox
控件的文本部分禁用键入.
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.
我最初的谷歌搜索结果显示了一个过于复杂、误导性的建议来捕获 KeyPress
事件.
My initial googling of this turned up an overly complex, misguided suggestion to capture the KeyPress
event.
推荐答案
要使 ComboBox 的文本部分不可编辑,请将 DropDownStyle 属性设置为DropDownList".ComboBox 现在本质上是仅供用户选择的.您可以在 Visual Studio 设计器或 C# 中执行此操作,如下所示:
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;
链接到 ComboBox DropDownStyle 的文档属性在 MSDN 上.
Link to the documentation for the ComboBox DropDownStyle property on MSDN.
这篇关于如何在 .NET 中使 ComboBox 不可编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!