ComboBox trouble
By rickvdbosch
- 2 minutes read - 256 wordsThe default ComboBox has some weird characteristics…
We created our own control which derives from the ComboBox because we wanted to create a ComboBox where the user could type (parts of) the value he or she wants to select, but would not be able to type something that was not in the list. So that’s kind of a combination between the DropDownStyles DropDown and DropDownList. The basics weren’t that complicated, so we created the controls pretty fast. But then came the next step: fine-tuning.
For instance: when you change the selected index of a ComboBox in code while it is dropped down and then set DroppedDown to false, the index is not the value to which you set it in code. But the text is set to the text of the item you set the index of! Because of this, the control shows the right text, but when you read that property, you get a different value. All of this while the Selected Index Changed event fires when you set the index in code…
So in stead of setting the selected index while the ComboBox is DroppedDown, you should set DroppedDown to false, change the selected index and then set DroppedDown to true again (if necessary). We encountered some more of this weird stuff, like this:
Setting the selected index to -1 doesn’t always reset your ComboBox. When you set the selected index to -1 twice, it works… :SAlso see this post on MSDN Library about that.
Have you got some weird stuff about the .NET ComboBox? Drop a line in the comments.