问题
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