2
votes

I'm having an issue with my ComboBoxes whereby if I type into it to get a value & then tab out the Text changes to the first item in the list with the first letter typed.

I have:

  • AutoCompleteMode set to SuggestAppend
  • AutoCompleteSource set to ListItems
  • DropDownStyle set to DropDownList

I add the items for the ComboBox in the Load event of the Form the ComboBox is on.

e.g. the below is code from a Load event where I populate a ComboBox that I have set up as below.

   `Me.ComboBox1.Text = ""
    Me.ComboBox1.Items.Add("a")
    Me.ComboBox1.Items.Add("aaa")
    Me.ComboBox1.Items.Add("combo")
    Me.ComboBox1.Items.Add("combobox")
    Me.ComboBox1.Items.Add("combobox test")
    Me.ComboBox1.Items.Add("common")
    Me.ComboBox1.Items.Add("common dialog")` 

After running the code, if I select the ComboBox1 & type in common - common is selected in ComboBox1 but if I leave ComboBox1 the Text reverts to combo.

It gets a bit stranger as if I user the below code in the ComboBox1_Leave event procedure it throws common:

MsgBox(ComboBox1.Text)

I've also tried assigning the value of Text to a string in the ComboBox1_KeyUp event procedure & then assign that to ComboBox1.Text in the ComboBox1_Leave event procedure but that doesn't do anything.

If I put a the above MsgBox code before assigning the strings value to ComboBox1.Text then the Text value does revert to Common but this is isn't a practical solution.

I've also noticed that if I hit Enter before hitting tab it retains the correct value but again I'm don't think this is a particularly practical solution.

Does anyone have any idea what's going on here & how I can fix it?

1

1 Answers

1
votes

It is absolutely necessary to have the DropDownStyle set to DropDownList?

Because if you set DropDownStyle to DropDown the selected value will be retained when you press tab or lose the focus.

If it's absolutely necessary to have it that way, you could try this.

Public Class Form2

  Dim selectedTextForCombo As String = ""

  Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Me.ComboBox1.Text = ""
    Me.ComboBox1.Items.Add("a")
    Me.ComboBox1.Items.Add("aaa")
    Me.ComboBox1.Items.Add("combo")
    Me.ComboBox1.Items.Add("combobox")
    Me.ComboBox1.Items.Add("combobox test")
    Me.ComboBox1.Items.Add("common")
    Me.ComboBox1.Items.Add("common dialog")
  End Sub

  Private Sub ComboBox1_LostFocus(sender As Object, e As System.EventArgs) Handles ComboBox1.LostFocus
    ComboBox1.SelectedItem = selectedTextForCombo
    'This is just for a visualization of your issue
    'Label1.Text = selectedTextForCombo
  End Sub

  Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    selectedTextForCombo = ComboBox1.Text
    'This is just for a visualization of your issue
    'Label1.Text = selectedTextForCombo
  End Sub

End Class

Warning:

  • This example works with the tab action.
  • If the users writes something that doesn't exist like "commun" the selected value will end up being the visually selected value, in this case: "common"