Display ComboBox "Cue Banner" when DropDownStyle = DropDownList
I have a ComboBox. I want it to allow select only listed Items. How can I have it display an initial legend?
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
ComboBox1.Items.Add("FirstItem")
ComboBox1.Items.Add("SecondItem")
ComboBox1.Items.Add("ThirdItem")
ComboBox1.SelectedText = "Select Item"
With this code the ComboBox displays with no text.
If I remove the ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
sentence then the text in ComboBox1.SelectedText = "Select Item"
开发者_开发问答is displayed, but the user can input other values in the combobox that not match the listItems.
Indeed, it is quite unfortunate that you're still stuck targeting Windows XP. The native Windows implementation of this (only available under Vista and later) is really quite slick. I'm not really sure what to recommend in that case. I've actually looked at this exact same problem before, and there's just no way to get the native Windows implementation to work on a combobox with the DropDownList
style set.
If you're set on getting this functionality in Windows XP, then you're going to have to write your own implementation in code, which is guaranteed to be ugly, I'm sorry to say. As you've discovered, the SelectedText
property has no effect on a combobox with the DropDownList
style set. You will have to actually add an item with the value "Select Item", ensure that it is selected by default, and then remove that item from the list after the user makes their first selection.
I would probably have to recommend that you use the native cue banner support where it is available (when the app is running on Vista or later), and only fall back to your custom implementation where it is absolutely necessary (on XP).
But if it were me, I'd punt the problem and take a different approach entirely. Here are a couple of ideas:
The poor man's approach, used long before these new-fangled "cue banners" were ever invented, is to simply place a
Label
control next to the combobox. In that label, you can display whatever instruction text is necessary. It may not be quite as slick or elegant, but it's just as functional.Simply set pre-select the default value, the one that the user is most likely to select in the majority of cases. The trick with a combobox set as a
DropDownList
is that once one of the items from the list is selected, there's no way for the user to ever unselect one. That is, they can't clear the existing selection without choosing a new item from the list. (You can certainly do so programmatically, but that's irrelevant in this case.) So if you pre-select a default value, it'll be obvious what they have to do (pick one), and they won't ever be able to clear the selection to where it is unobvious again.I think in almost all cases, there is a sensible default. But even where there's not (say, "Choose your gender: Male/Female"), you could simply select one or the other as the default, and users will be savvy enough to change it if it's incorrect for them. This is how I do it in all of my applications.
The only way to pre-select a value in a
DropDownList
, of course, is through code. Use something like the following to select the first item in the list:myComboBox.SelectedIndex = 0
I know this question is a couple years old and has an answer, but I had the same requirements and came up with a different solution. I thought I should link to my solution in case anyone else comes across this. It allows you to add a cue banner to "read-only" combo boxes in XP. The combo boxes use the default style (which isn't read-only), but you can do some things in code and the designer to make them function as read-only and still keep your cue banner. See here for the details: https://stackoverflow.com/a/17869453/2340643
精彩评论