Solved

Retrieve Index of Matching value in Combobox

Posted on 2009-03-31
11
704 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Sheritlw
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you again... You really helped me out!
0
 

Author Closing Comment

by:Sheritlw
Comment Utility
Thanks for the great answer
0
 

Expert Comment

by:rmsoftjsr
Comment Utility
What ? if My Two customer in DisplayMember have identical name but different id.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 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