Shibli
Shibli

Reputation: 6139

Win Forms: Add a "void" item to ComboBox

Is there a way to add an item which has no text and even if that item selected it would be shown that nothing selected at all or SelectedIndex would be smaller than zero?

Upvotes: 0

Views: 186

Answers (2)

Andrei G
Andrei G

Reputation: 1590

You can add an item that is represented by an empty string, sure. But SelectedIndex will return the selected index of the item. Excerpt from MSDN: A value of negative one (-1) is returned if no item is selected.

You should consider working with the value of the item instead of its index.

Upvotes: 1

Jonathan Wood
Jonathan Wood

Reputation: 67193

No, the combo box does not support this. There is no built-in logic for treating an item as non-selectable.

Sounds like you will need to either implement an owner draw combo box, or perhaps even a new control from scratch to make this work. I'm sure there are third-party controls that would do what you need as well.

Depending on what you are trying to accomplish (you didn't provide much detail), the ListView control might also provide some options for you.

Upvotes: 1

Related Questions