It looks like when you have the WinForms.NET and ComboBox applications (installed in the DropDown style), and that the ComboBox has several elements in it that are identical, strange things happen. In particular, the index of the selected item may change without triggering the SelectedIndexChanged event.
Of course, this causes massive confusion and strange, unclear errors, due to which I recently pulled my hair.
Here is a simple example that you can use to find out what I'm talking about:
- Create a new .NET WinForms project (I use VB.NET, but feel free to translate - it's easy enough).
- Drop the ComboBox, button and TextBox (set MultiLine = True) to the form.
- Use the following code to load a ComboBox with three identical elements, and to print some status messages when the SelectedIndexChanged event occurs, and to view the currently selected index (using the button):
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox1.Text = TextBox1.Text & vbNewLine & "ComboBox SelectedIndexChanged event fired." & vbNewLine & _
"SelectedIndex is: " & ComboBox1.SelectedIndex
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ComboBox1.Items.Add("John Doe")
ComboBox1.Items.Add("John Doe")
ComboBox1.Items.Add("John Doe")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text & vbNewLine & _
"Button clicked." & vbNewLine & _
"SelectedIndex is: " & ComboBox1.SelectedIndex
End Sub
Run the project and select an item from ComboBox (say, middle). Then click the combobox drop-down arrow, but DO NOT CHOOSE ANYTHING. Click the button (Button1 by default) and see what it says.
If I havenโt lost my mind, here is what you should see:
ComboBox SelectedIndexChanged event fired.
SelectedIndex is: 1
Button clicked.
SelectedIndex is: 0
In other words, the SELECTED INDEX is CHANGED, but without triggering the SelectedIndexChanged event!
, ComboBox . , . ( , ComboBox "DropDown" "DropDownList".)
, โโ .NET, -, , , - , ( !), , ! ( , SelectedIndex , , , , - !)