Click here to Skip to main content
Licence CPOL
First Posted 19 Jun 2006
Views 19,776
Downloads 129
Bookmarked 29 times

Parent-Child ComboBoxes

By | 19 Jun 2006 | Article
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:

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.

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)

About the Author

Yildirim Kocdag

Web Developer

Turkey Turkey

Member

Yildirim Kocdag is a MSc.BSc.Computer Engineer from Istanbul. He is working as a .NET consultant. Currently using c#, vb.net, asp.net, javascript, SQL and Oracle. His Favourite areas in Computer Science are Compilers, Expert Systems, Digital Image Processing, AI and Extreme Programming.
ykocdag@yahoo.com


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberaaroncampf15:59 25 Jan '11  
GeneralBEATIFULL PinmemberNazmiDUMAN20:07 18 Jan '07  
GeneralHi PinmemberVertyg08:40 19 Jun '06  
GeneralRe: Hi [modified] PinmemberYildirim Kocdag19:52 19 Jun '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 19 Jun 2006
Article Copyright 2006 by Yildirim Kocdag
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid