(VB) combobox with United States values RRS feed

  • Question

  • I've found some old code to make a combobox capable of auto complete and filled with each state. I'd like to avoid typing it line by line. How can I get this code to work? 
    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Enabled = True
            Dim vState As String
            'Combo box is filled with state abbreviations
            For Each vState In Array("ME", "NH", "VT", "MA", "CT", "RI", "NY", "VA", "NC",
        "SC", "GA", "FL", "DE", "NJ", "OH", "MI", "IL", "IN", "IA", "KS", "NE", "OK",
        "TX", "AL", "TN", "MO", "ND", "SD", "WY", "MT", "ID", "NV", "WA", "OR", "CO",
        "NM", "AZ", "WV", "PA", "CA", "AR", "HI", "MN", "WI", "MD", "MS", "AK", "LA",
        "UT", "KY")

    Thursday, December 6, 2018 10:59 PM

All replies

  • Hi

    Here is some code that may be helpful.

    NOTE: I have included a .Sort on the list so if not needed then just remove the line.

    ' Form1 with ComboBox named 'cmbState'
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim states As New List(Of String)({"ME", "NH", "VT", "MA", "CT", "RI", "NY", "VA", "NC", "SC", "GA", "FL", "DE", "NJ", "OH", "MI", "IL", "IN", "IA", "KS", "NE", "OK", "TX", "AL", "TN", "MO", "ND", "SD", "WY", "MT", "ID", "NV", "WA", "OR", "CO", "NM", "AZ", "WV", "PA", "CA", "AR", "HI", "MN", "WI", "MD", "MS", "AK", "LA", "UT", "KY"})
        ' remove if sort not needed
        With cmbState
          .DataSource = states
          .AutoCompleteMode = AutoCompleteMode.SuggestAppend
          .AutoCompleteSource = AutoCompleteSource.ListItems
        End With
      End Sub
    End Class

    Regards Les, Livingston, Scotland

    • Edited by leshay Thursday, December 6, 2018 11:18 PM
    • Proposed as answer by KareninstructorMVP Friday, December 7, 2018 12:08 PM
    Thursday, December 6, 2018 11:18 PM
  • This worked perfectly, Thank you!
    Friday, December 7, 2018 12:36 AM
  • Hi,

    We appreciate that you shared your solution with us. Please mark the post(s) that helped you as the answer.

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 7, 2018 1:01 AM