Solved

Frustrated try to Loop through combobox VB.Net

Posted on 2009-04-01
8
1,639 Views
Last Modified: 2012-06-27
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


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

Open in new window

0
Comment
Question by:Sheritlw
  • 5
  • 3
8 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 24036600
why re-invent the wheel for finding a specific item in a combobox?  The control already has the functionality built in....

// This will find the first item with "TEST" as it's value.
Dim index As Integer = comboBox1.FindStringExact("TEST")
// this will select the first item that has "TEST" as it's value.
comboBox1.SelectedIndex = index
0
 

Author Comment

by:Sheritlw
ID: 24036642
I tried the solution provided again as follows
i = cbo.FindStringExact(iValue.ToString)
it returns -1
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24036749
Can you give some example of values you see in your combobox and what value you are searching for ?
0
 

Author Comment

by:Sheritlw
ID: 24036767

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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Sheritlw
ID: 24036772
My company table holds value 4 in its StateID field.
I need the combo to select id 4 and show Arizona.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 24036778
Perhaps this can help you
Dim dt As DataTable = DirectCast(comboBox1.DataSource, DataTable)
Dim yourState as string
For i As Integer = 0 To dt.Rows.Count - 1
    Dim displayText As String = dt.Rows(i)(comboBox1.DisplayMember).ToString()
    Dim valueItem As String = dt.Rows(i)(comboBox1.ValueMember).ToString()
    ' Process the object depending on the type
    if valueItem = "TheValueYouAreSearchingFor" then
       comboBox1.SelectedIndex = i
       yourState = displayText
   end if
Next
0
 

Author Comment

by:Sheritlw
ID: 24036821
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
0
 

Author Closing Comment

by:Sheritlw
ID: 31565203
Excellent.  Gave me the perfect example.
Thanks for your help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now