學生系統優化——字符限定

一、文本框限定

1、限定輸入的字符長度

文本框中有個MaxLength屬性,輸入自己要限定的數字即可;也可以利用代碼來限定,當超過限定長度時,彈出警告對話窗

Private sub txtUserName_change()
    If Len(Trim(txtUserName)) > 4 then
        MsgBox "姓名不能超過4個字符,請重新輸入!", 0 + 48
        txtUserName = ""
    End if 
End iSub

Trim函數是消除字符串的空格,Len函數是計算字符串的長度

2、限定特殊字符

Private Sub txtUserName_Change()
    If Len(Trim(txtUserName)) > 4 then
        MsgBox "姓名不能超過4個字符,請重新輸入!", 0 + 48
        txtUserName = ""
    End if 
End sub

3、限定輸入的字符類型

Private Sub txtSID_KeyPress(KeyAscii As Integer)
    Const xStr As String = "123456"    '只能輸入數字
    KeyAscii = IIf(Instr(xStr & Chr(8), Chr(KeyAscii)), KeyAscii, 0)
End Sub

4、限定特殊字符

Private Sub txtDirector_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
        Case 8     '限制退格鍵
        Case Asc("A") To ("Z")
        Case Asc("a") To ("z")
        Case Is < 0
        Case Else
            KeyAscii = 0
            MsgBox "格式錯誤,請輸入漢字或英文!", 0 + 48
            txtDirector.Text = ""
    End Select
End Sub

二、下拉框限定 

Combox限定不能鍵盤輸入,只能選擇下拉框裏面的內容

Private Sub comboGrade_KeyPress(KeyAscii As Integer)
    KeyAscii = 0        '限制鍵盤不能輸入內容
End Sub

三、限定成績

Private Sub txtResult_Change()
    If Val(txtResult.Text) > 120 Or Val(txtResult.Text) < 0 Then
        MsgBox "請輸入成績在0~120範圍內!", 0 + 48
        txtResult.Text = ""
    End If
End Sub

四、限定不能複製粘貼

在第二次輸入密碼時,不能複製粘貼

Private Sub txtPassword1_KeyDown(KeyCide As Integer, Shift As Integer)
    If (KeyCode = 86 Or KeyCode = 67 Or KeyCode = 88) And Shift = 2 Then
        MsgBox "不能複製粘貼", 0 + 48
        txtPassword1.Text = ""
    End If
End Sub

 

 

 

 

 

  

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