none
how to use state city dropdownlist in windows based application ? RRS feed

All replies

  • can you give me source code for this problem

    Hi

    Here is some code that illustrates one way to use a ComboBox with AutoComplete and SuggestAppend.

    Needs a new project with a ComboBox1 and Label1 added in the Designer, then copy/replace all Form1 code with this code.

    ' Form1 with ComboBox1 and Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	' set up the ComboBox so that when
    	' the user starts to type, a list
    	' of matching States is presented
    	With ComboBox1
    	  ' add some test data to combobox1
    	  .Items.AddRange({"Texas", "Alabama", "California", "Arkansas", "Washington", "Ohio", "Alaska", "Colorado", "Arizona", "Connecticut"})
    
    	  ' set up the matching list dropdown
    	  .DropDownStyle = ComboBoxStyle.DropDown
    	  ComboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    	  .AutoCompleteSource = AutoCompleteSource.ListItems
    	End With
      End Sub
      Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    	Dim cb As ComboBox = DirectCast(sender, ComboBox)
    
    	Dim SelectedState As String = cb.SelectedItem.ToString
    	' here, you have the state name as selected
    
    	'put selectedstate name into Label1
    	Label1.Text = SelectedState
    
    	' do something based on which State
    	' the user has selected
    	Select Case SelectedState
    	  Case "California"
    		' California was selected
    		' so do something funky 
    	  Case "Arkansas"
    		' California was selected
    		' so do something funny 
    	  Case Else
    		' not in list was selected
    		' so do something crazy 
    	End Select
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Sunday, February 11, 2018 1:34 PM
  • We have more on our website, this one we can only still use one year

    http://www.vb-tips.com/ComboboxRelatedDataViewDistinct.ASPX


    Success Cor

    Sunday, February 11, 2018 1:42 PM
  • Okay I misread that you wanted this in a windows based application as a dropdown is not native to a windows base application while a ComboBox is.

    If we were dealing with data in a database it would be prudent to have a state and a city table e.g.

    We need to read in the two tables as shown below except you would not use TOP 10 in the state table and would not use the IN clause in the city table. TOP and IN were used because my tables have a huge amount of data.

    This code is SQL-Server but the same can be done with Oracle, ms-acccess etc. Note the TRIM is used because someone else added data and had some rows with spaces at the beginning of names.

    Imports System.Data.SqlClient
    
    Public Class Operations
    
        Private ConnectionString As String =
            "Data Source=KARENS-PC;Initial Catalog=ForumExample;" &
            "Integrated Security=True"
    
        Private mStateTable As DataTable
        Public ReadOnly Property StateTable As DataTable
            Get
                Return mStateTable
            End Get
        End Property
        Private mCityTable As DataTable
        Public ReadOnly Property CityTable As DataTable
            Get
                Return mCityTable
            End Get
        End Property
        Private Sub LoadStatesCityTables()
            mStateTable = New DataTable
            mCityTable = New DataTable
    
            Using cn As New SqlConnection With {.ConnectionString = ConnectionString}
                Using cmd As New SqlCommand With {.Connection = cn}
    
                    cmd.CommandText = "SELECT TOP 10 Id,LTRIM(Name) AS name " &
                        "FROM dbo.StateMaster ORDER BY Name"
    
                    cn.Open()
    
                    mStateTable.Load(cmd.ExecuteReader)
    
                    cmd.CommandText = "SELECT ID,LTRIM(Name) AS name,StateID " &
                                      "FROM dbo.CityMaster " &
                                      "WHERE StateId IN (3352,3119,3423,1433,269,2012,2013,3804,3805,1301) " &
                                      "ORDER BY Name"
    
                    mCityTable.Load(cmd.ExecuteReader)
                End Using
            End Using
        End Sub
        Public Sub New()
            LoadStatesCityTables()
        End Sub
    End Class

    Form code

    Public Class Form1
        Private cityTable As DataTable
        Private Sub Form1_Load(sender As Object, e As EventArgs) _
            Handles MyBase.Load
    
            Dim ops As New Operations
    
            cboStates.DataSource = ops.StateTable
            cboStates.DisplayMember = "Name"
    
            cityTable = ops.CityTable
            cboCity.DataSource = cityTable
            cboCity.DisplayMember = "Name"
    
            AddHandler cboStates.SelectedIndexChanged,
                AddressOf cboStates_SelectedIndexChanged
    
            GetCities()
    
        End Sub
    
        Private Sub cboStates_SelectedIndexChanged(sender As Object, e As EventArgs)
            GetCities()
        End Sub
        Private Sub GetCities()
            Dim stateId = CType(cboStates.SelectedItem, DataRowView).
                Row.Field(Of Integer)("Id")
    
            cityTable.DefaultView.RowFilter = $"StateId = {stateId}"
    
        End Sub
    End Class

    Run time examples


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Sunday, February 11, 2018 3:43 PM
    Moderator