ARTICLE

Implementing Parent-Child ComboBoxes in VB.NET

Posted by Yildirim Kocdag Articles | Windows Forms VB.NET June 23, 2006
Parent-Child ComboBoxes is one of the classical problems for almost all projects. This article shows how to implement such comboboxes.
Download Files:
 
Reader Level:

Parent-Child ComboBoxes is one of the classical problems for almost all projects. As a brief of this problem is,

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

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor