How to make Combobox in winforms readonly

后端 未结 18 1941
攒了一身酷
攒了一身酷 2020-12-08 09:40

I do not want the user to be able to change the value displayed in the combobox. I have been using Enabled = false but it grays out the text, so it is not very

相关标签:
18条回答
  • 2020-12-08 10:07

    The best thing I can suggest is to replace the combo-box with a read-only textbox (or just perhaps a label) - that way the user can still select/copy the value, etc.

    Of course, another cheeky tactic would be to set the DropDownStyle to DropDownList, and just remove all other options - then the user has nothing else to pick ;-p

    0 讨论(0)
  • 2020-12-08 10:12

    You can change the forecolor and backcolor to the system colors for an enabled combo box, although this may confuse the users (why have it if they can't change it), it will look better.

    0 讨论(0)
  • 2020-12-08 10:13

    Why don't you just use a text box? Text box has a "Read only" property, and since you want your combo box only to display data, I don't see why you would need a combo box.

    An alternative is that you just cancel out the input for the "on value changed" event. That way you will be displaying your information no mater what user does ...

    0 讨论(0)
  • 2020-12-08 10:18

    enter link description here

    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.

    0 讨论(0)
  • 2020-12-08 10:18

    Set DropdownStyle Property to Simple

    Add below code to to KeyPress event of ComboBox

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

    Add below code to to KeyDown event of ComboBox

    private void comboBoxName_KeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = true;
        return;
    }
    
    0 讨论(0)
  • 2020-12-08 10:18

    Here is the Best solution for the ReadOnly Combo.

    private void combo1_KeyPress(object sender, KeyPressEventArgs e) {
        e.KeyChar = (char)Keys.None; 
    } 
    

    It will discard the keypress for the Combo. It doesn't have "e.KeyChar" !

    0 讨论(0)
提交回复
热议问题