Readonly ComboBox in WinForms

点点圈 提交于 2019-11-27 07:34:36

问题


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).


回答1:


Set DropDownStyle to "DropDownList"




回答2:


Set the ComboBox.DropDownStyle property to ComboBoxStyle.DropDownList.




回答3:


Another simple way to go about it.

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



回答4:


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



回答5:


Try using a DropDownListbox



来源:https://stackoverflow.com/questions/162936/readonly-combobox-in-winforms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!