学生系统优化——字符限定

一、文本框限定

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

5、只能输入数字

If keyAscii = 8 Then Exit Sub
If keyAscii < 48 Or keyAscii > 57 Then keyAscii = 0

6、限制数字大小,这个适合成绩对话框

Private Sub txtResult_change()
On Error Resume Next
If Val(Trim(txtResult.Text)) > 100 Then
MsgBox "输入数字过大,请重新输入"
txtResult.Text = ""
End If
End Sub

 7、不能输入特殊字符(和第二、四个一样的性质)

Select Case keyAscii
Case -20319 To -3652
Case 48 To 57
Case 65 To 98
Case 97 To 122
Case 8
Case Else
keyAscii = 0
End Select

 8、限定特殊字符、数字、空格,只能输入汉字和字母

Private Sub txtCourseName_KeyPress(KeyAscii As Integer)
    If KeyAscii < 0 or KeyAscii = 8 or KeyAscii = 13 Then
    Else If
        Not chr(KeyAscii) Like "[a-ZA-Z]" Then
        keyAscii = 0
    End if 
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

提示密码剩余次数

If miCout = 1 Then
    MsgBox "您还有两次机会", 0 + 48,"提示"
Exit sub
End If

If miCout = 2 Then
    MsgBox "您还有一次机会", 0 + 48,"提示"
Exit sub
End If

If miCout = 3 Then
    MsgBox "即将关闭程序", 0 + 48,"提示"
    Me.Hide
Exit sub
End If

  

猜你喜欢

转载自blog.csdn.net/weixin_45490023/article/details/107032896
今日推荐