Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / Visual Basic

Parent-Child ComboBoxes

Rate me:
Please Sign up or sign in to vote.
2.60/5 (8 votes)
19 Jun 2006CPOL1 min read 43.9K   470   29   4
Parent-child ComboBoxes is one of the classical problems in most projects.

Introduction

Parent-child ComboBoxes is one of the classical problems in most projects. A brief overview of this problem is:

  1. There are two combos.
  2. One of them is the parent and the other is the child combo.
  3. 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.

Sample screenshot

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.

Sample screenshot

About Code

The query of the user control which fills the data source can take a parameter with the '@' character. This is illustrated below:

SQL
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:

Sample screenshot

The two pivotal properties are Query, ComboFired, and the pivotal procedure is for the selected value changed event.

VB
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

License

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


Written By
Architect
Turkey Turkey
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

Comments and Discussions

 
GeneralMy vote of 5 Pin
aaroncampf25-Jan-11 15:59
aaroncampf25-Jan-11 15:59 
O.o SOO COOL!!!
GeneralBEATIFULL Pin
NazmiDUMAN18-Jan-07 20:07
NazmiDUMAN18-Jan-07 20:07 
GeneralHi Pin
Vertyg019-Jun-06 8:40
Vertyg019-Jun-06 8:40 
GeneralRe: Hi [modified] Pin
Yildirim Kocdag19-Jun-06 19:52
Yildirim Kocdag19-Jun-06 19:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.