title: .NET WinForms ComboBox, identical items, and the SelectedIndexChanged event
body: <p>It seems like when you have a WinForms .NET application, and a ComboBox (set to the "DropDown" style), and that ComboBox has multiple items in it that are identical, weird things happen. Specifically, the index of the selected item can change <em>without</em> firing the SelectedIndexChanged event. </p>
  
  <p>Of course, this causes mass confusion and weird, obscure errors, which is what I've been pulling my hair out over lately.</p>
  
  <p>Here's a simple example you can use to see what I'm talking about:</p>
  
  <ul>
  <li>Make a new .NET WinForms project (I use VB.NET, but feel free to translate - it's simple enough).</li>
  <li>Drop a ComboBox, a button, and a TextBox (set MultiLine=True) onto the form.</li>
  <li>Use the following code to load the ComboBox with 3 identical items and to print some status messages when the SelectedIndexChanged event fires, and to see what the currently selected index is (via a button):</li>
  </ul>
  
  <pre><code>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</code></pre>
  
  <p>Run the project and select an item from the ComboBox (say, the middle one). Then, click the ComboBox's drop-down arrow, but DON'T SELECT ANYTHING. Click the Button (Button1 by default) and see what it says.</p>
  
  <p>Unless I've lost my mind, here's what you should see:</p>
  
  <pre>ComboBox SelectedIndexChanged event fired.
  SelectedIndex is: 1
  Button clicked.
  SelectedIndex is: 0</pre>
  
  <p>In other words, the SELECTED INDEX HAS CHANGED but without the SelectedIndexChanged event firing!</p>
  
  <p>This only happens when the items in the ComboBox are identical. If they're different, this doesn't happen. (It also doesn't happen if the ComboBox's "DropDown" style is set to "DropDownList.")</p>
  
  <p>I suspect this may be a bug in the .NET framework itself and not something I can fix, but on the off chance that anyone else has any ideas on what to do here (or what I might be doing wrong!), please chime in! I'm at a loss to explain this behavior or work around it (I expect the SelectedIndex to stay the same unless, y'know, you actually CHANGE it by selecting something else!)</p>