1
votes

Using C#, say you have a ComboBox that has a DropDownStyle set to DropDown (there are items in the drop down but the user can manually enter a value as well). How do you set a default value for the ComboBox that is not in the list of values in the drop down, but begins with text from a possible selection? Normally setting ComboBox.Text works fine, but if there is an item in the drop down that begins with the text you want as the default, it automatically selects the first item in the list that starts with the text. For example:

Values in the drop down:
c:\program files\
c:\windows\
d:\media\

Default Value Assignment
myComboBox.Text = "C:\";

Result
Initial value of ComboBox when the form opens is "c:\program files\".

So what am I doing wrong? How do I correctly set a default value of an item not in the drop down list that begins with a possible selection?

3

3 Answers

1
votes

Does the following code work?

myCombo.SelectedIndex = myCombo.FindString(@"c:\");

Note: I haven't tried it. Looked up for properties/methods that could help using reflector.

0
votes

I was able to get this to work with having the items in the ComboBox be ComboBoxItems (I dont see why this wouldn't work with other types). Set the ComboBox.Text like you are and make sure SelectedIndex = -1 and also you need IsEditable = True.

0
votes

I couldn't repro the behavior you are describing. Adding the three values via the Items collection and then setting the initial value to "c:\" (you omitted an @ in your code sample, by the way) worked fine. My guess is that something else in your code is setting the value of the combo box after you set it.