【原创】vb.net 动态控件 事件添加

【原创】vb.net 动态控件 事件添加

这里写图片描述
添加选项后:
这里写图片描述
为实现动态添加,所需代码如下(共三个函数):

    Private selectcond1 As String = ""  '厂家
    Private selectcond2 As String = ""  '工程队
    Private selectcond3 As String = ""  '浮动标志
    Private selectcond4 As String = ""  '是否出账
    ''' <summary>
    ''' 选择项变化响应函数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cobCJ_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cobCJ.SelectedIndexChanged, cobGCD.SelectedIndexChanged, cobFDBZ.SelectedIndexChanged, cobCZ.SelectedIndexChanged
        '厂家
        If Not cobCJ.Text.ToLower.StartsWith("system") And cobCJ.Text.ToString <> "" And sender Is cobCJ Then
            If cobCJ.Text.Trim <> "" Then cbCJ.Checked = True
            flp1_add(cobCJ.Text.ToString)
            Dim x As Int16 = InStr(selectcond1, cobCJ.Text.ToString)
            If x > 0 Then
                selectcond1 = selectcond1.Remove(x - 1, cobCJ.Text.Length + 1)
            Else
                selectcond1 += cobCJ.Text & ";"
            End If
        ElseIf String.IsNullOrEmpty(cobCJ.Text.Trim) Then
            cbCJ.Checked = False
        End If
        '工程队
        If Not cobGCD.Text.ToLower.StartsWith("system") And cobGCD.Text <> "" And sender Is cobGCD Then
            If cobGCD.Text.Trim <> "" Then cbGCD.Checked = True
            flp1_add(cobGCD.Text.ToString)
            Dim x As Int16 = InStr(selectcond2, cobGCD.Text.ToString)
            If x > 0 Then
                selectcond2 = selectcond2.Remove(x - 1, cobGCD.Text.Length + 1)
            Else
                selectcond2 += cobGCD.Text & ";"
            End If
        ElseIf String.IsNullOrEmpty(cobGCD.Text.Trim) Then
            cbGCD.Checked = False
        End If
        '浮动标志
        If Not cobFDBZ.Text.ToLower.StartsWith("system") And cobFDBZ.Text <> "" And sender Is cobFDBZ Then
            If cobFDBZ.Text.Trim <> "" Then cbFDBZ.Checked = True
            If String.IsNullOrEmpty(selectcond3) Then
                selectcond3 = "浮动:" & cobFDBZ.Text
                flp1_add(selectcond3)
            Else
                flp1_add(selectcond3)
                selectcond3 = "浮动:" & cobFDBZ.Text
                flp1_add(selectcond3)
            End If
        ElseIf String.IsNullOrEmpty(cobFDBZ.Text.Trim) Then
            cbFDBZ.Checked = False
            flp1_add(selectcond3)
            selectcond3 = ""
        End If
        '出账
        If Not cobCZ.Text.ToLower.StartsWith("system") And cobCZ.Text <> "" And sender Is cobCZ Then
            If cobCZ.Text.Trim <> "" Then cbCZ.Checked = True
            If String.IsNullOrEmpty(selectcond4) Then
                selectcond4 = "出账:" & cobCZ.Text
                flp1_add(selectcond4)
            Else
                flp1_add(selectcond4)
                selectcond4 = "出账:" & cobCZ.Text
                flp1_add(selectcond4)
            End If
        ElseIf String.IsNullOrEmpty(cobCZ.Text.Trim) Then
            cbCZ.Checked = False
            flp1_add(selectcond4)
            selectcond4 = ""
        End If
    End Sub
    ''' <summary>
    ''' 控件动态添加函数,向FlowLayoutPanel控件flp1中动态添加子控件
    ''' </summary>
    ''' <param name="text"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function flp1_add(ByVal text As String) As Boolean
        If text.ToLower.StartsWith("system") Or String.IsNullOrEmpty(text.Trim) Then
            Return False
            Exit Function
        End If
        For i As Int16 = 0 To flp1.Controls.Count - 1
            If flp1.Controls.Item(i).Text = text Then
                RemoveHandler flp1.Controls.Item(i).MouseDoubleClick, AddressOf labclicked_DoubleClick  '指定子控件鼠标双击响应函数
                flp1.Controls.RemoveAt(i)
                Return False
                Exit Function
            End If
        Next
        Dim netctl As Control = New Label
        netctl.Text = text
        netctl.BackColor = Color.GreenYellow
        flp1.Controls.Add(netctl)
        AddHandler netctl.MouseDoubleClick, AddressOf labclicked_DoubleClick     '指定子控件鼠标双击响应函数
        Return True
    End Function
    ''' <summary>
    ''' 子控件鼠标双击响应函数
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Sub labclicked_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim lab As Label = sender
        Dim str As String = lab.Text
        flp1_add(str)
        '厂家
        Dim x As Int16 = InStr(selectcond1, str)
        If x > 0 Then
            selectcond1 = selectcond1.Remove(x - 1, str.Length + 1)
            If String.IsNullOrEmpty(selectcond1) Then
                cbCJ.Checked = False
                cobCJ.Text = ""
            End If
        End If
        '工程队
        x = InStr(selectcond2, str)
        If x > 0 Then
            selectcond2 = selectcond2.Remove(x - 1, str.Length + 1)
            If String.IsNullOrEmpty(selectcond2) Then
                cbGCD.Checked = False
                cobGCD.Text = ""
            End If
        End If
        '浮动标志
        x = InStr(str, "浮动:")
        If x > 0 Then
            selectcond3 = ""
            cbFDBZ.Checked = False
            cobFDBZ.Text = ""
        End If
        '出账
        x = InStr(str, "出账:")
        If x > 0 Then
            selectcond4 = ""
            cbCZ.Checked = False
            cobCZ.Text = ""
        End If
    End Sub
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页