Introduction
Parent-child ComboBoxes is one of the classical problems in most projects. A brief overview of this problem is:
- There are two combos.
- One of them is the parent and the other is the child combo.
- After the parent one is fired (selected value changed), the child combo should use the selected value of the parent to fill its datasource.
For instance, there are three combos below: country, city, and district. After the country combo is selected, the others should be filled.
Generally, the difficulty can be solved by writing a procedure for 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 with the best solution. The two important properties of such a User Control will be Query
and also ComboFired
.
About Code
The query of the user control which fills the data source can take a parameter with the '@' character. This 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 code or selected value events for the form. The only code will be written in the form load event, as shown below:
The two pivotal properties are Query
, ComboFired
, and the pivotal procedure is for the selected value changed event.
Public Property Query() As String
Get
Return m_Query
End Get
Set(ByVal Value As String)
m_Query = Value
If Me.DesignMode Then
Exit Property
End If
If m_Query.IndexOf("@") <> -1 Then
Exit Property
End If
If m_Query <> "" Then
datacontrol = False
dtCombo.Rows.Clear()
dtCombo = SQLData.dondur_datatable(m_Query)
ComboBox1.DataSource = dtCombo
ComboBox1.ValueMember = "DEGER"
ComboBox1.DisplayMember = "ETIKET"
datacontrol = True
Dim sender As Object
Dim e As System.EventArgs
SV(sender, e)
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
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)
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
Exit Sub
End If
Dim real_str() As String
real_str = Split(nextCombo.Query, "=")
If Not datacontrol Then
Exit Sub
End If
nextCombo.Query = real_str(0) & "=" & CStr(ComboBox1.SelectedValue)
End If
End Sub
Phd. Yildirim Kocdag is a Computer Engineer.
Programming Languages
Android, Objective-c, c#, vb.net, asp.net, javascript, TSQL.
Computer Science
DataMining, Compilers, Expert Systems, Digital Image Processing, AI and Extreme Programming.
ykocdag@yahoo.com
http://www.linkedin.com/profile/view?id=223886830