1
votes

I have a piece of code where a ComboBox in DropDown mode on a form has as its datasource a fixed ArrayList of unique items; usually this works just fine for users, but very rarely, an error occurs where the SelectedIndex fails to the item corresponding to the item represented by the ComboBox text property entered by the user (even when it is a legitimate item).

To be more specific, a user types a legitimate entry as text in the ComboBox text field and navigates away to another control using the mouse. The text-field continues to show the entry but at some later point, when the user commits the changes using a method that depends on the SelectedIndex corresponding to the text being show, the SelectedIndex is incorrect (on occasion).

The official documentation http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.text.aspx states:

"Setting the Text property to null or an empty string ("") sets the SelectedIndex to -1. Setting the Text property to a value that is in the Items collection sets the SelectedIndex to the index of that item. Setting the Text property to a value that is not in the collection leaves the SelectedIndex unchanged."

So legitimate text entries should move the SelectedIndex to the proper number. I know I can force a consistency check into an event handler before anything important happens, but I was wondering what could be causing this rare bug which flies in the face of the documentation to better understand what ComboBox is actually doing. I'd appreciate any help.

2
I was just wondering the language you use to do the code. What error do you get? Being a little more specific would go a long way in answering your question.Ha Sh
so what is your question?spajce
My question is what is the best practice when it comes to using combobox and is there some setting I'm neglecting to set that would cause this error?Beastian
My first thought is CAPS inconsistency. I would guess that "item1" is not the same as "Item1". Could this be your problem in case it isn't smart enough to catch this.WozzeC
There is a log of the output in terms of an outlook email body and a database entry. The email body contains the combobox text correctly (caps and all) but the database entry which relies on the SelectedIndex is incorrect. This is how I came to my diagnosis.Beastian

2 Answers

3
votes

Right, after some testing this is my conclusion.

When you are typing in the textfield of a combobox you are not actually triggering the Index selection. However, if you set the Text property to a value. i.e. Combobox1.Text = "Existing item" then it sets the selected index. So it seems that you really shouldn't have any correct results in your database if they type instead of selecting in combobox.

Right, so this is where it gets stupid. This will work:

Private Sub ComboBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.TextChanged
    ComboBox1.Text = ComboBox1.Text
End Sub

I would however recommend you to do this instead:

Private Sub ComboBox1_Leave(sender As System.Object, e As System.EventArgs) Handles ComboBox1.Leave
    ComboBox1.Text = ComboBox1.Text
End Sub

To make my answer differ some I'll add this.

If you add these settings for the combobox then typing will work without the leave event.

    ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    ComboBox1.AutoCompleteSource = AutoCompleteSource.ListItems
2
votes

When the ComboBox is in DropDown Mode, the SelectedIndexChanged even is not fired when the user does not select from the items in the list. Hence, when the user simply types an entry in and then navigates away using the mouose, the selectedIndex is not changed. In order for this to work, you need to manually handle the ComboBox.Leave event such that any text typed in by the user is then selected.

Private Sub ComboBox1_Leave(sender As Object, e As System.EventArgs) Handles ComboBox1.Leave
    ' This will cause the SelectedIndex to be changed, thus firing the Selected_IndexChanged Event:
    ComboBox1.SelectedIndex = ComboBox1.FindStringExact(ComboBox1.Text)
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    MsgBox("SelectedIndex =" & ComboBox1.SelectedIndex.ToString())
End Sub