Ian Ringrose
Ian Ringrose

Reputation: 51917

How do I have an item in a WinForm ComboBox that the user cannot select?

I have a combo box with a drop down style of ComboBoxStyle.DropDownList, as well as letting the user choose options it shows the current state. So sometimes I wish it to show “unknown” but the user should never be able to choose “unknown” from the dropdown list.

Upvotes: 0

Views: 61

Answers (2)

Marco Guignard
Marco Guignard

Reputation: 653

I would set the DropDownStyle Property to DropDown and then just set the Text of the ComboCox to "unknow" (or an empty string).

You could check user input with the SelectedIndex Property (which should be -1, when the text is not corresponding from the list).

Upvotes: 0

Gerald Versluis
Gerald Versluis

Reputation: 34013

I guess most elegant would be to overload your ComboBox, and ComboBoxItem.

Add a property to the ComboBoxItem if it should be selectable or not, and on the ComboBox index changed event check the property and reject the selection if it shouldn't be selected. It also enabled you to draw the nonselectable ComboBoxItem different, or give it a different font to make it clear to the user.

Upvotes: 1

Related Questions