在做学生系统是有好多的输入框,每个输入框输入的内容都不要相同,有的要求输入姓名,学号,班级,家庭住址等。因为有需求所以要限制输入的内容,纯数字的输入,纯文本的输入等。这就需要我们运用之前学过的代码或者相对应的属性来限制。
下面主要来说一下限制的方法:
代码类型的限制,其实大部分的输入是我们用键盘来敲进去的字符,所以我们就可以用ASCII码来限制输入的内容了。
'班号信息只能是数字
Private Sub txtClassno_KeyPress(KeyAscii As Integer) If KeyAscii = 8 Then Exit Sub If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 MsgBox "非法字符,只能输入数字! ", vbOKCancel, "提示" End If If Len(txtClassno) >= 3 Then txtClassno.Text = "" '输入框内容清零 KeyAscii = 0 '输入的第四个字符清零 MsgBox "输入数值位数过长", vbOKCancel, "提示" End If End Sub '不支持特殊符号的输入 Private Sub txtClassroom_KeyPress(KeyAscii As Integer) If (KeyAscii >= 33 And KeyAscii <= 47) Or (KeyAscii >= 58 And KeyAscii <= 64) Or (KeyAscii >= 91 And KeyAscii <= 96) Or (KeyAscii >= 123 And KeyAscii <= 126) Then(注意:其中有一些标点符有圆角和半角的区分大家遇到例如:全角的“,”“。”“·”) KeyAscii = 0 MsgBox "不支持特殊字符输入!", vbOKCancel, "提示" End If End Sub '只能输入字母和数字 If KeyAscii = 8 Then Exit Sub If (KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Then Else KeyAscii = 0 MsgBox "非法字符,只能输入字母或数字! ", vbOKCancel, "提示" End If If Len(txtold) >= 6 Then txtold.Text = "" '输入框内容清零 KeyAscii = 0 '输入的第四个字符清零 MsgBox "输入的密码位数过长", vbOKCancel, "提示" End If
输入框输入限制修改到frmmodifycourseinfo“修改课程信息”窗口