Search code examples
comboboxcontrolsselectedindex

vb.net selectedindex.text empty while it's not


I have a form with combo boxes that show strings that were retrieved with a query from the database.

When I click on one of the combo boxes I invoke the ComboBox.Click event which goes out and does a query and shows all possible values one can choose from.

Here is were it starts acting strange. I put a statement in the code like:

Dim ac As ComboBox = DirectCast(sender, ComboBox) 
MsgBox("selected text " & ac.SelectedText)

This shows an empty messagebox, but when I put a breakpoint right after this code and check the contents it has a value.

debug

And it is happening only on the first combobox on the form and only when i click in the combobox. If I click on the dropdown arrow it gives the correct string in the messagebox. If I first click another combobox and then this combobox it works correct.

Anyway, what I am trying to do is that when a user clicks in the combobox the index that is shown does not change. What I noticed is that the selectedindex is always -1 on the first instance. So that's why I want to save the text value to look it up in the list of values and set the index to the correct position.

If anyone has a better alternative I am happy to hear it.


Solution

  • Never mind. I changed the combobox click event into a combobox gotfocus event instead. This seems to work for both clicking in the combobox and clicking on the combobox dropdown arrow.