简体   繁体   English

WinForms中的只读ComboBox

[英]Readonly ComboBox in WinForms

I'm writing a GUI in C#, Visual Studio 2008, using the Designer and WinForms. 我正在使用Designer和WinForms在C#,Visual Studio 2008中编写GUI。 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. 我有一个ComboBox控件,我希望它仅允许从提供的选项中进行选择,而不接受用户输入的字符串。 It doesn't appear to have a ReadOnly property, and disabling it hinders the readability of the control (as well as disallowing user-selection). 它似乎没有ReadOnly属性,并且禁用它会影响控件的可读性(以及不允许用户选择)。

将DropDownStyle设置为“ DropDownList”

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

Another simple way to go about it. 另一种简单的方法。

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

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

尝试使用DropDownListbox

My requirement : once user giving an input through combo-box they can not change the value before submit it. 我的要求:一旦用户通过组合框输入,他们就无法在提交之前更改该值。 They can read only that value. 他们只能读取该值。

As per my requirement i do the following things. 根据我的要求,我做了以下事情。

1) Get input from user through combo-box. 1)通过组合框从用户获取输入。
2) Copy the value of combo-box to a text-box(which is read only and invisible). 2)将组合框的值复制到文本框(只读且不可见)。
3) False the visibility of combo-box. 3)假组合框的可见性。
4) True the visibility of read only text-box. 4)真实只读文本框的可见性。

Do this with events. 用事件做这件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM