|
-
August 4th, 2008, 11:14 AM
#1
Combobox Parent-Child Question
I have 3 comboboxes (ex. State, County, City). I have code in the Combobox_SelectionChangeCommitted function to load the child combobox when the Parent is selected. If there's only 1 item loaded into the child combobox, I want that to be selected automatically.
Assuming that there's only 1 County per State and 1 City per County, the expected behavior is that I select a State and the County is automatically selected since there's only 1, then the City is automatically selected since there's only 1 city for the County that was selected.
The County list is getting loaded properly when I select a State, but when it's selected, the ChangeCommitted function is not firing, which means that the City is not getting loaded.
Is there a better way of doing this? I'm fairly new to VB.NET and used to doing things in VB6.
Code:
Private Sub cboState_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboState.SelectionChangeCommitted
cboCounty.Items.Clear()
cboCity.Items.Clear()
If cboState.SelectedIndex <> -1 Then
LoadCountiesByState(cboCounty)
End If
End Sub
Private Sub cboCounty_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboCounty.SelectionChangeCommitted
cboCity.Items.Clear()
If cboCounty.SelectedIndex <> -1 Then
LoadCitiesByCounty(cboCity)
End If
End Sub
The following functions are in a different code module and need to stay there since they are called by multiple locations within the project. Also, defaulting to the only entry needs to stay in here as well for the same reason.
Code:
Public Sub LoadCountiesByState(ByRef cbx as ComboBox, ByVal intStateID as Integer)
'Code to Load a list of Counties by State ID
For Each County in ...
cbx.Items.Add(New ListData(intCountyID, strCountyName))
Next
If cbx.Items.Count = 1 Then cbx.SelectedIndex = 0
End Sub
Public Sub LoadCitiesByCounty(ByRef cbx as ComboBox, ByVal intCountyID as Integer)
'Code to Load list of Cities by County ID
For Each City in ...
cbx.Items.Add(New ListData(intCityID, strCityName))
Next
If cbx.Items.Count = 1 Then cbx.SelectedIndex = 0
End Sub
I'd rather be wakeboarding...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|