亲子组合框

 

介绍 亲子组合盒是大

  

 

多数项目中的经典问题之一。这个问题的一个简要概述是: 有两种组合。其中一个是父组合,另一个是子组合。在父组合被触发之后(所选值被更改),子组合应该使用父组合的所选值来填充它的数据源。 例如,下面有三个组合:国家、城市和地区。选择国家组合后,其他的应该被填满。 一般来说,这个困难可以通过编写一个程序来解决组合框的选择值事件。但是,如果表单有4或5个组合,比如国家、城市、地区、街道号和公寓号,这可能会导致意大利面条代码。 一个基本的用户控件(这也是本文的主题)可以为编码器提供最好的解决方案。这种用户控件的两个重要属性是Query和ComboFired。 关于代码 填充数据源的用户控件的查询可以接受一个带有'@'字符的参数。如下图所示: 隐藏,复制Code

Select city_id as DEGER, city_name as ETIKET from cities where country_id=@

不需要为窗体编写任何代码行或选择值事件。在表单加载事件中只会编写代码,如下所示: 这两个关键属性是Query、ComboFired,关键过程是针对所选值更改事件的。 隐藏,收缩,复制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

本文转载于:http://www.diyabc.com/frontweb/news282.html

posted @ 2020-08-05 03:13  Dincat  阅读(112)  评论(0编辑  收藏  举报