Changing the index of combo box programmatically based on text box value

Hi, I am very new in vb.net  I will appreciate if someone help me. I have a databound textbox on the form and one combo box bound with arraylist. arraylist has two columns Code and description. Discription has been set as DisplayMember and Code as ValueMember. 
So during record navigation Whenever textbox value changed I want my combo box should change the index based on textbox code. I dont know how to search two column arraylist.  My arraylist is like this

"CO" "Correction Office"
"CP" "Computer Programmer"
"IA" "Insurance Agent"

While my textbox may have Value "CO" , "CP", "IA" or "" so based on textbox value I want to change the index on my combo box as 0,1,2 or -1



Answer this question

Changing the index of combo box programmatically based on text box value

  • Dave Balsillie

    When you say "ArrayList", what do you mean   The ArrayList object accepts generic Object datatype as Items.  I'm not sure from your post what you are using to hold the values that you add to the ArrayList.  What kind of object are you using to give the ArrayList "columns"

    Assuming that you have some custom object holding your values, and your combobox works with the databindings you setup, you could use something like this in your TextBox.TextChanged event:

            Dim IsFound As Boolean = False
            Dim Index As Integer = -1
            Dim enm As IEnumerator

            enm = myArrayList.GetEnumerator
            While enm.MoveNext
                Index += 1
                If enm.Current.Code = TextBox1.Text Then
                    IsFound = True
                    Exit While
                End If
            End While
            If Not IsFound Then Index = -1
            ComboBox1.SelectedIndex = Index

    This example uses "TextBox1" for the textbox the user types in, "ComboBox1" for the databound combobox, and "myArrayList" for the list of items (NOTE that myArrayList must be declared at the Form level).  To test this example, I added the following code to a form with a textbox and a combobox:

    'First, Create a Custom Class to hold our values.  Since you only need 2 properties for each item, you could use a builtin object, but I'm going to create a custom one for example purposes...

    Public Class myItem
        Private myCode As String
        Private myDescription As String

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal Code As String, ByVal Description As String)
            MyBase.New()
            myCode = Code
            myDescription = Description
        End Sub

        Public Property Code() As String
            Get
                Return myCode
            End Get
            Set(ByVal Value As String)
                myCode = Value
            End Set
        End Property

        Public Property Description() As String
            Get
                Return myDescription
            End Get
            Set(ByVal Value As String)
                myDescription = Value
            End Set
        End Property
    End Class

    'Now the code for our form
    Public Class Form1
        Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

        Dim myArrayList As New ArrayList

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            Dim nItem As New myItem("CO", "Correction Office")
            myArrayList.Add(nItem)
            nItem = New myItem("CP", "Computer Programmer")
            myArrayList.Add(nItem)
            nItem = New myItem("IA", "Insurance Agent")
            myArrayList.Add(nItem)

            ComboBox1.DataSource = myArrayList
            ComboBox1.DisplayMember = "Description"
            ComboBox1.ValueMember = "Code"


        End Sub

        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            Dim IsFound As Boolean = False
            Dim Index As Integer = -1
            Dim enm As IEnumerator

            enm = myArrayList.GetEnumerator
            While enm.MoveNext
                Index += 1
                If enm.Current.Code = TextBox1.Text Then
                    IsFound = True
                    Exit While
                End If
            End While
            If Not IsFound Then Index = -1
            ComboBox1.SelectedIndex = Index

        End Sub

    End Class

    Something like that will get it.  Hope this helps!

  • Yves1

    No problem!

    Glad I could help.  =)

  • tha0

    Thats it!, This is the code what I was looking for. Thank you very much. You were awsome 
  • Changing the index of combo box programmatically based on text box value