C# System.Windows.Forms.ComboBox Text does not update after a selection change

2.5k Views Asked by At

I am using a simple ComboBox in C#. I put in 3 items: "apple", "banana", and "orange". Here is what I do:

  1. Click on the comboBox to view the drop-down. I see all 3 items fine. Then I select "banana" via mouse-left-click
  2. The comboBox now shows "banana" which is correct so far
  3. Now I click on the comboBox again to view the drop-down. I move my mouse to highlight "orange". I then hit TAB to move off the comboBox
  4. The comboBox is still showing "banana"
  5. However, when I get the selectedItem, it's "orange"

Is this the expected behaviour? If I hightlight "orange" and tab out, I would expect either:

  1. selectedItem should remain the same (meaning tab out does not trigger a selection change) or...
  2. the comboBox should display "orange" since the selection has changed

I overcome this by using the comboBox.Leave event handler to manually set the text field to be the selectedItem however, I would like to know if I am missing some fundamental concepts here.

It seems logical that if selection changes, the comboBox.Text should reflect this without any manual intervention?

1

There are 1 best solutions below

0
On

I believe you have it figured out correctly. In a combobox, the selecteditem does not have to be the same as the text. This allows non-items to be entered in the text of the combobox. You can just ignore the SelectedItem and use the text field.