Solved

Retrieve Index of Matching value in Combobox

Posted on 2009-03-31
11
706 Views
Last Modified: 2012-06-27
Hi EE,

I am wanting a procedure that will look through items in a combo box to see which one matches the id value I need and then set the index of the item as the selected item in the combo.
I created something similar to what I used to use in VB 6, but it doesn't work.
What am I doing wrong?
I've also included the code I use to fill the combobox, just in case it's needed.
Thanks
Public Function ListFind(ByVal cbo As ComboBox, ByVal iValue As Integer) As Integer

        Dim i As Integer
 

        For i = 0 To cbo.Items.Count - 1

            If cbo.Items(i) = iValue Then

                cbo.SelectedIndex = i

                Exit For

            End If
 

        Next
 

    End Function
 
 

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.GetStrTableName = msTblName

            cFields.ComboTable = msTblName

            SelectStr = cFields.ReturnComboString()

            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)

                cnt.ValueMember = cFields.SendToCorrectFieldProcedure(1)
 

            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
  • 2
  • +1
11 Comments
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24036325
So the data into your combo are integers ? (If cbo.Items(i) = iValue Then)
If yes I suppose that cFields.SendToCorrectFieldProcedure(1) returns an integer, doesn't it ?
So after all those statement, could you explain what doesn't work exactly ? For example, into the ListFind function, do you have all the data into cbo when you set a breakpoint there ?
0
 
LVL 13

Expert Comment

by:kaylanreilor
ID: 24036333
Concerning what is inside cbo (the type of the data) check also that, even if they are supposed to be integers, they are not actually strings !
Thus you could try: If cbo.Items(i) = iValue.ToString() Then...
0
 

Author Comment

by:Sheritlw
ID: 24036339
The cFields.SendtoCorrectFieldProcedure brings back the primary field name, ie. StateID.
The error is Operator " =" is not defined for type datarowview and type integer.
0
 

Author Comment

by:Sheritlw
ID: 24036425
Unfortunetely, That doesn't work.
Doesn't anyone know how to loop through a combobox values and get the primary id so that you can match it up with a value.
For example, I have a company table that has a related field StateID.  When I fill the company form, i also get the stateid for the company.  I then fill my combos.  Now I need to find out what item in the list has the same stateID so I can make it the selected item.

In VB 6, it was so EASY...
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24036607
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

Source: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.findstringexact.aspx
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: 24036631
The problem is, I need to find the value of the Valuemember that holds the Integer StateID.
I don't have the text value of the state (the state name)
I also tried using the find string but it didn't work.
Please advise.
Thanks
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 24036773
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
       yourState = displayText
   end if
Next
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
ID: 24036839
Perhaps this can help you -- setting the combobox.selectedindex
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: 24036845
Thank you again... You really helped me out!
0
 

Author Closing Comment

by:Sheritlw
ID: 31565174
Thanks for the great answer
0
 

Expert Comment

by:rmsoftjsr
ID: 34840547
What ? if My Two customer in DisplayMember have identical name but different id.
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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

920 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

12 Experts available now in Live!

Get 1:1 Help Now