前端之家收集整理的这篇文章主要介绍了
【原创】vb.net 动态控件 事件添加,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
【原创】vb.net 动态控件 事件添加
添加选项后:
为实现动态添加,所需代码如下(共三个函数):
Private selectcond1 As String = ""
Private selectcond2 As String = ""
Private selectcond3 As String = ""
Private selectcond4 As String = ""
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
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
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
原文链接:https://www.f2er.com/vb/257428.html