Question Combobox FindString and Selected Index

Cobalt

Member
Joined
May 28, 2008
Messages
21
Programming Experience
3-5
Hello all!

I'm using a combox Findstring function to return the index location of an item in the list. The search string is created from a Database query that all works correctly. I then set the SelectedIndex to the value found.

VB.NET:
Dim SearchString as String = MyDataTable.Row(1).Item("User").ToString

Dim index As Integer = ComboBox1.FindString(SearchString)

If index <> -1 then
    ComboBox1.SelectedIndex = index
End if

At this point in the Application all works as it should.

Later in the code (on a Button click) I need to read in the value that is currently in Combobox1. No matter what option I pick (SelectedItem, Index, Text, Value etc) it keeps coming back as either blank or a null reference exception.

The only way I can get around this is to actually click on the Combobox, I don't even need to change the option, just expand the Combobox.

If I do this, the Button Click works and I get the value in the Combobox.

Going forward, this is not suitable for my end users as I cannot expect them to click on the combobox for no reason.

I have tried Combobox1.Focus when the Button is clicked to try and trick it into thinking the Combo has been clicked but with no change.

Is there anyway I can get around this and reference a Combobox item when the system does not think it has been selected?

Huge thanks to anyone who can figure this one out!

If you need more code or explinantion, please let me know!

Rob
 

Cobalt

Member
Joined
May 28, 2008
Messages
21
Programming Experience
3-5
Well after about 2 days of working on it, I think I've sorted it. I needed to use the .Text value and not any of the .Selected... items.

Very odd. Hope this thread is of use to someone!
 
Top Bottom