1. 程式人生 > >減少程式碼冗餘 機房

減少程式碼冗餘 機房

一:防止往下拉選單輸入:

Private Sub comboRelation2_KeyPress(KeyAscii As Integer) '防止輸入字元
    KeyAscii = 0     'keyAscii=0?表示:取消本次輸入的字元
End Sub

二:定義一個檢查文字框是否為空的函式:

在模組中設函式:

Public Function testtxt(txt As String) As Boolean         '引數是txt,即文字框內容
    If Trim(txt) = "" Then
       testtxt = False
    Else
       testtxt = True
    End If
End Function

 呼叫函式判斷是不是為空(不僅僅限用文字框)

If Not testtxt(Combouser.Text) Then
    MsgBox "請選擇使用者級別!", vbOKOnly + vbExclamation, "提示"
    Combouser.SetFocus
    Exit Sub
End If

三:設定case1:

Case1:
    With MSHFlexGrid1
        .Rows = 1
        .TextMatrix(0, 0) = "序列號"
        .TextMatrix(0, 1) = "教師"
        .TextMatrix(0, 2) = "級別"
        .TextMatrix(0, 3) = "註冊日期"
        .TextMatrix(0, 4) = "註冊時間"
        .TextMatrix(0, 5) = "登出日期"
        .TextMatrix(0, 6) = "登出時間"
        .TextMatrix(0, 7) = "機器名"
        .TextMatrix(0, 8) = "狀態"
    End With

在同一個事件下面直接呼叫:GoTo Case1

四:限制特殊字元輸入:

在模組中定義這個函式:

Public Function IsNumber(KeyAscii As Integer) As Integer
    Select Case KeyAscii
        Case 48 To 57    '只能輸入數字
        Case 65 To 90    '只能輸入大寫字母
        Case 97 To 122   '只能輸入小寫字母
        Case 8           '只能輸入退格
        Case Else       
        KeyAscii = 0     '無效
    End Select
        If Len(txtID) >= 3 Then    
           txtID.Text = ""  
           KeyAscii = 0    '輸入字元清零
           MsgBox "輸入字元位數過長", vbOKCancel, "提示"
        End If
End Function

在窗體中呼叫函式:

Private Sub txtid_KeyPress(KeyAscii As Integer)
Call IsNumber(KeyAscii)
End Sub

不能輸入特殊字元,也不能超過三個數。