Parent-Child ComboBoxes is one of the classical problems for almost all projects. As a brief of this problem is,
- There are two combos,
- One of them is parent and another is child combo.
- After the parent one is fired(selected value changed), child combo should be taken the selected value of the parent and filled its datasource by the help of this parameter.
For instance, there are three combos below; country,city and district. After the country combo selected the others should be filled.
Generally, the difficulty can be solved by writing a procedure to the selected value event of the combobox. However, this can cause spaghetti code, if the form has 4 or 5 combos, such as country, city, district, street number and apartment number.
A Basic User Control which is also the topic of this article can provide the coder best solution. The important properties of this user control can be Query and also ComboFired.
The Query of the user control which fills the datasource can take a parameter with the "@" character. It is illustrated below;
Select
city_id as DEGER, city_name as ETIKET from cities where country_id=@ It is not necessary to write any line of the code or selected value events to the form, the only code will be written in the form load such as below.
The two pivotal properties and one procedure are query, combofired and SV(selected value changed).
About Code
Public Property Query() As String
Get
Return m_Query
End Get
Set(ByVal Value As String)
m_Query = Value
If Me.DesignMode Then ' checks if it is in design mode
Exit Property
End If
If m_Query.IndexOf("@") <> -1 Then ' finds if the query consists any parameters
Exit Property
End If
If m_Query <> "" Then
datacontrol = False ' locks the firing selected value change event
dtCombo.Rows.Clear()
dtCombo = SQLData.dondur_datatable(m_Query) ' fills th datasource
ComboBox1.DataSource = dtCombo
ComboBox1.ValueMember = "DEGER"
ComboBox1.DisplayMember = "ETIKET"
datacontrol = True ' unlocks the firing selected value change event
Dim sender As Object
Dim e As System.EventArgs
SV(sender, e) ' after the combo is filled, fires the selected value event
End If
End Set
End Property
Public Property ComboFired() As String
Get
Return m_ComboFired
End Get
Set(ByVal Value As String)
m_ComboFired = Value
If Me.DesignMode Then
Exit Property
End If
If m_ComboFired <> "" Then ' adds the selected value event
AddHandler ComboBox1.SelectedValueChanged, AddressOf SV
End If
End Set
End Property
Protected Sub SV(ByVal sender As Object, ByVal e As System.EventArgs)
If Not Form1_init Then
Exit Sub
End If
If ComboBox1.SelectedIndex <> -1 Then
Dim nextCombo As New UCCombo
nextCombo = findThecombo(m_ComboFired) ' finds the child combo
If nextCombo Is Nothing Then
Exit Sub
End If
If (nextCombo.Query Is Nothing) Then
Exit Sub
End If
If (nextCombo.Query.IndexOf("=") = -1) Then ' checks for parameters
Exit Sub
End If
Dim real_str() As String
real_str = Split(nextCombo.Query, "=")
If Not datacontrol Then
Exit Sub
End If
' creates the new query
nextCombo.Query = real_str(0) & "=" & CStr(ComboBox1.SelectedValue)
End If
End Sub