开发者

How do I set combobox read-only or user cannot write in a combo box only can select the given items? [duplicate]

This question already has answers here: How can I make a ComboBox non-editable in .NET? 开发者_如何学Go (7 answers) Closed 7 years ago.

I am facing a problem in setting the combo property such that only user can select the values form given items, but I cannot write in the combo box.

How can I do so in C#?


Just change the DropDownStyle to DropDownList. Or if you want it completely read only you can set Enabled = false, or if you don't like the look of that I sometimes have two controls, one readonly textbox and one combobox and then hide the combo and show the textbox if it should be completely readonly and vice versa.


I think you want to change the setting called "DropDownStyle" to be "DropDownList".


In the keypress event handler:

e.Handled = true;


Make the DropDownStyle to DropDownList

stateComboBox.DropDownStyle = ComboBoxStyle.DropDownList;


Try this:

    private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        // comboBox1 is readonly
        e.SuppressKeyPress = true;
    }


The solution is to change the DropDownStyle property to DropDownList. It will help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜