vb動態加載控件

Option Explicit
Dim con As New ADODB.Connection
Dim res As New ADODB.Recordset
Dim moveX As Long '動態添加控件時控件移到X點
Dim moveY As Long '動態添加控件時控件移到Y點
Const mWidth = 1500
Const ConCount = 4 '表中查詢條件的數量
Const WM_DELETE = &H400 '自定義消息
Const GWL_WNDPROC = -4
Dim LVW_COUNT As Integer 'listview控件中行數
Dim WithEvents cboxName As VB.ComboBox
Dim WithEvents cboxAge As VB.ComboBox
Dim WithEvents cboxTime As VB.ComboBox
Dim isLoadAge(2) As Boolean
Dim tBoxName As VB.TextBox
Dim tBoxAge(1) As Control
Dim tBoxTime() As VB.TextBox
Dim lbl(1) As VB.Label
Private Sub Age_Click() '選中年齡
    Dim itemA As ListItem
    Dim i, x As Integer
    res.MoveFirst
    Do While Not res.EOF
        If (res.Fields("Condition")) = "年齡" Then
            For x = 1 To ListViewSearch.ListItems.Count
                If ListViewSearch.ListItems(x).Text = "年齡" Then
                    MsgBox "該配置項中已經存在!"
                Exit Sub
                End If
            Next x
            Set itemA = ListViewSearch.ListItems.Add(, , res.Fields("Condition"))
            Set cboxAge = Controls.Add("VB.ComboBox", "cboxAge")
            LVW_COUNT = ListViewSearch.ListItems.Count
            moveX = ListViewSearch.Width + 60
            moveY = ListViewSearch.Top + 260 * (LVW_COUNT)
            cboxAge.Visible = True
            cboxAge.Move moveX, moveY, mWidth
            For i = 1 To ConCount
                If res.Fields(i).Value <> "" Then
                    cboxAge.AddItem res.Fields(i).Value
                End If
            Next i
        End If
        res.MoveNext
        Set itemA = Nothing
    Loop
End Sub
Private Sub cboxAge_Click() '選擇年齡查詢條件,根據所選條件查詢
    If Not (tBoxAge(0) Is Nothing) Then
        Dim wParam As Long
        Dim lParam As Long
        Dim lResult As Long
        SendKeys "{Enter}"
        Call SendMessage(Me.hwnd, WM_DELETE, wParam, lParam)
    End If
    If cboxAge.List(cboxAge.ListIndex) = "介於兩者之間" Then
       Set tBoxAge(0) = Controls.Add("VB.TextBox", "tBoxAge0") '添加控件用於輸入年齡進行查詢
       Set tBoxAge(1) = Controls.Add("VB.TextBox", "tBoxAge1")
       Set lbl(0) = Controls.Add("VB.Label", "lbl")
       isLoadAge(0) = True
       isLoadAge(1) = True
       lbl(0).Caption = "到"
       lbl(0).Visible = True
       tBoxAge(0).Visible = True
       tBoxAge(1).Visible = True
       moveX = ListViewSearch.Width + cboxAge.Width + 50
       moveY = cboxAge.Top
       tBoxAge(0).Move moveX, moveY, mWidth, cboxAge.Height - 50
       moveX = moveX + tBoxAge(0).Width + 50
       lbl(0).Width = mWidth - 1200
       lbl(0).Move moveX, moveY + 50, lbl(0).Width, tBoxAge(0).Height
       tBoxAge(1).Move moveX + lbl(0).Width - 100, moveY, mWidth, cboxAge.Height
   Else
      Set tBoxAge(0) = Controls.Add("VB.TextBox", "tBoxAge0")
      isLoadAge(0) = True
      tBoxAge(0).Visible = True
      moveX = ListViewSearch.Width + cboxAge.Width + 50
      moveY = cboxAge.Top
      tBoxAge(0).Move moveX, moveY, mWidth, cboxAge.Height
   End If
End Sub

Private Sub cboxAge_KeyPress(KeyAscii As Integer)
       If KeyAscii = 13 Then
         Controls.Remove tBoxAge(0)
         Set tBoxAge(0) = Nothing
       End If
       KeyAscii = 0
End Sub

Private Sub Form_Load()
    con.ConnectionString = "Provider=SQLOLEDB;Persist Security Info=False;User ID=sa;PWD=密碼;Initial Catalog=數據庫名;Data Source=服務器名" '連接數據庫字符串
    con.Open
    con.CommandTimeout = 20
    res.Open "CaseSerchConfg", con, adOpenDynamic, adLockPessimistic
    ListViewSearch.View = lvwReport '報表顯示
    ListViewSearch.ColumnHeaders.Add 1, , "Condition", 2050
    ListViewSearch.GridLines = True
    Call WindowProc(Me.hwnd, WM_DELETE, 0, 0)
End Sub
Private Sub Name_Click() '選中姓名
    Dim itemA As ListItem
    Dim i, x As Integer
    res.MoveFirst
    Do While Not res.EOF
        If (res.Fields("Condition")) = "姓名" Then
            For x = 1 To ListViewSearch.ListItems.Count
                If ListViewSearch.ListItems(x).Text = "姓名" Then
                    MsgBox "該配置項中已經存在!"
                Exit Sub
                End If
            Next x
            Set itemA = ListViewSearch.ListItems.Add(, , res.Fields("Condition"))
            Set cboxName = Controls.Add("VB.ComboBox", "cboxName")
            Set tBoxName = Controls.Add("VB.TextBox", "tBoxName")
            LVW_COUNT = ListViewSearch.ListItems.Count
            moveX = ListViewSearch.Width + 60
            moveY = ListViewSearch.Top + 260 * (LVW_COUNT)
            cboxName.Visible = True
            cboxName.Move moveX, moveY, mWidth
            For i = 1 To ConCount
                If res.Fields(i).Value <> "" Then
                    cboxName.AddItem res.Fields(i).Value
                End If
            Next i
            tBoxName.Visible = True
            moveX = moveX + cboxName.Width
            tBoxName.Move moveX, moveY, mWidth, cboxName.Height
        End If
        res.MoveNext
    Loop
    Set itemA = Nothing
End Sub

Private Sub Time_Click() '選中日期
    Dim itemA As ListItem
    Dim i, x As Integer
    res.MoveFirst
    Do While Not res.EOF
        If (res.Fields("Condition")) = "時間" Then
            For x = 1 To ListViewSearch.ListItems.Count
                If ListViewSearch.ListItems(x).Text = "時間" Then
                    MsgBox "該配置項中已經存在!"
                Exit Sub
                End If
            Next x
            Set itemA = ListViewSearch.ListItems.Add(, , res.Fields("Condition"))
            Set cboxTime = Controls.Add("VB.ComboBox", "cboxTime")
            cboxTime.Visible = True
            LVW_COUNT = ListViewSearch.ListItems.Count
            moveX = ListViewSearch.Width + 60
            moveY = ListViewSearch.Top + 260 * (LVW_COUNT)
            cboxTime.Move moveX, moveY, mWidth
            For i = 1 To ConCount
                If res.Fields(i).Value <> "" Then
                    cboxTime.AddItem res.Fields(i).Value
                End If
            Next i
        End If
        res.MoveNext
    Loop
    Set itemA = Nothing
End Sub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章