Sheritlw
asked on
Frustrated try to Loop through combobox VB.Net
Hi EE,
Getting really frustrated. I have search everywhere trying to find an example of how to loop through items in a combobox. It was so EASY in VB 6.
1. I have a table Company that has a StateID
2. I fill the table and capture the StateID value
3. I fill my State combobox using the code below.
Now I need to find which item in the list has the same stateid.
I tried all kinds of things, but nothing works.
Please help
Thank you
Getting really frustrated. I have search everywhere trying to find an example of how to loop through items in a combobox. It was so EASY in VB 6.
1. I have a table Company that has a StateID
2. I fill the table and capture the StateID value
3. I fill my State combobox using the code below.
Now I need to find which item in the list has the same stateid.
I tried all kinds of things, but nothing works.
Please help
Thank you
Public Sub FillComboBox(ByVal cnt As ComboBox)
'example fill states
Dim ds As New DataSet
If DatabaseToUse() = BizObjs.DBType.Access Then
'Access Database
Dim cFields As New cFieldTableNames
cFields.ComboTable = msTblName
SelectStr = cFields.ReturnComboString() 'Returns my select statement
Dim dc As New OleDb.OleDbConnection(ConnectionString)
Dim da As New OleDb.OleDbDataAdapter(mSelectStr, dc)
Try
da.Fill(ds)
dc.Close()
cnt.DataSource = ds.Tables(0)
cnt.DisplayMember = cFields.SendToCorrectFieldProcedure(3) 'Gets the field name for the display member
cnt.ValueMember = cFields.SendToCorrectFieldProcedure(1) ' Get the field name (StateID) for the valuemember
Catch ex As Exception
MessageBox.Show(ex.Message)
Throw ex
Finally
dc.Close()
da.Dispose()
End Try
End If
End Sub
ASKER
I tried the solution provided again as follows
i = cbo.FindStringExact(iValue .ToString)
it returns -1
i = cbo.FindStringExact(iValue
it returns -1
Can you give some example of values you see in your combobox and what value you are searching for ?
ASKER
Yes, I am search for
IDs are autonumber fields in Access
ID 1 goes with Alaska
ID 2 goes with Alabama
ID 3 goes with Arkansa
ID 4 goes with State Arizona
ID 5 goes with California
ID 6 goes with Colorado
etc.
thanks
ASKER
My company table holds value 4 in its StateID field.
I need the combo to select id 4 and show Arizona.
I need the combo to select id 4 and show Arizona.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi Dhaest,
It worked!!!! Thank you, thank you, thank you.
I have another question open that asks the same thing. It is titled ...
"Retrieve Index of Matching value in Combobox"
Put something in there and I will give you those points to.
Thanks again
It worked!!!! Thank you, thank you, thank you.
I have another question open that asks the same thing. It is titled ...
"Retrieve Index of Matching value in Combobox"
Put something in there and I will give you those points to.
Thanks again
ASKER
Excellent. Gave me the perfect example.
Thanks for your help!
Thanks for your help!
// This will find the first item with "TEST" as it's value.
Dim index As Integer = comboBox1.FindStringExact(
// this will select the first item that has "TEST" as it's value.
comboBox1.SelectedIndex = index