Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good day guys,

how to do I write a function in vb.net for combobox that will help populate multiple combobox for state and another function that will populate another comboboxes for cities based on each state selected from state combobox. I have this function to handle combobox for state.
VB
Private Sub PopulateStateCombo(ByVal cbox As ComboBox)
        cbox.Items.Add("Newyork")
        cbox.Items.Add("California")
        cbox.Items.Add("Texas")
        cbox.Items.Add("Arizona")
End Sub

which I callup like PopulateStateCombo(ComBoxState).

Thank you in anticipation.
Posted
Comments
[no name] 10-Jul-15 19:04pm    
You research "cascading combobxes"

1 solution

below is a sample code as to how you can achieve your goal.
To use the code create a new windowsApplication, then drag two combobox(name it cmbState and cmbCity) to the form(form1) then paste the code to form1

(Note: don't get confuse by the amount of code.
The actual implementation is in cmbState_SelectionChangeCommitted() and form1_load() events.
The generateData() simply generates data to be fed to the combobox)

 Dim stateDT As DataTable 'in memory table for storing state data (datatable can easily be filled with data from your Database)
    Dim cityDt As DataTable ' in memory table for storing city data (datatable can easily be filled with data from your Database)
    'bindingSource has a filter property which we will make use of to filter the cityDT as per the state chosen
    Dim cityBS As BindingSource

 ''' <summary>
    '''  initialize and fill stateDT and cityDt with state and city data respectively
    '''  note: to be able to chose city from the state chosen we need a connection between these two data groups and we 
    ''' have 'stateSno' in both citydt and statedt as the connecting column
    ''' </summary>
    ''' <remarks></remarks>
    Sub generateData()
        stateDT = New DataTable
        stateDT.Columns.Add("stateSno", GetType(Integer))
        stateDT.Columns.Add("stateName", GetType(String))
        stateDT.Rows.Add(0, "Newyork")
        stateDT.Rows.Add(1, "California")

        cityDt = New DataTable
        cityDt.Columns.Add("stateSno", GetType(Integer)) 'this is a related column to stateDt->stateSNo ,so we just need to use filter
        cityDt.Columns.Add("cityName", GetType(String))

        'cities for Newyork
        cityDt.Rows.Add(0, "NY_city1")
        cityDt.Rows.Add(0, "NY_city2")

        'cities for California
        cityDt.Rows.Add(1, "Cal_city1")
        cityDt.Rows.Add(1, "Cal_city2")
        cityDt.Rows.Add(1, "Cal_city3")
    End Sub

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

        'here we feed state data to the State combobox using datasource property
        cmbState.DataSource = stateDT 'feed state data directly to combobox
        cmbState.DisplayMember = "stateName" 'the member displayed in the combobox text
        cmbState.ValueMember = "stateSno" 'the value returned when we call combobox.selectedvalue

        cityBS = New BindingSource 'I suggest you get use to using binding source if you need to filter or sort data
        cityBS.DataSource = cityDt 'feed city data to cityBS
        cmbCity.DataSource = cityBS ' then feed cityBS to the city combobox so that we can use cityBS's filter property
        cmbCity.DisplayMember = "cityName" 'the member displayed in the combobox text

        'explicitly set state to index 0 and call cmbState_SelectionChangeCommitted() to filter city as the per the state selected
        cmbState.SelectedIndex = 0
        cmbState_SelectionChangeCommitted(sender, e)
    End Sub

 Private Sub cmbState_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles cmbState.SelectionChangeCommitted
        If cmbState.SelectedIndex < 0 Then
            cityBS.Filter = "stateSNo = -1" 'if no item is selected then we set it to a state no.(in this case -1) which we know will never be assigned to a state
        Else
            cityBS.Filter = "stateSNo = " & cmbState.SelectedValue 'simply filter cityBS with the cmbState selectedValue to get the desired result 
        End If
    End Sub
 
Share this answer
 
v4
Comments
Ibpet11 11-Jul-15 4:58am    
Thank you Richard, but what I discover is that other state combobox has same value as the first one i have selected also same to the city combobox. this is still the issue I wanted resolve.
Richard chiu 11-Jul-15 5:35am    
sorry, I am not not sure what you are trying to point out. Is it a problem with the code I have posted or you have other issue besides the initial one you have posted.
Ibpet11 11-Jul-15 11:31am    
No problem with the code you posted Richard. It worked very fine. But bcos I have many state combobox and City combobox in that apps I dont want to repeat code but rather want a way to call up functions to populate the City combobox. Now using your code whatever I select in the first State Combobox appears for every other state Combobox.
Richard chiu 12-Jul-15 21:04pm    
What is your PuRPOSE of having so many state cOMbobox on a SingLe page I am asking to see lf I can proVIde A more APRROPriate solution
by the way you could simply provide A bindingsource for each city and in the selectioncHAngEcomMittED event you simply use if or SwitCH case to filter the AppROPRIate binding source as per the sender name
Ibpet11 16-Jul-15 9:56am    
Thank you Richard for your assistance. I eventually get is solved.
I actually used a tab for registration of student, staff on same form.
So I created a procedure to populate state from StateTable from page load, and another procedure to get StateID which will be triggered onces I select a state from the StateComboBox to be compare with CityTable and load the cities for each state selected.

Thank you once again.

I can actually share the code should anybody need it even you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900