在敲学生的时候需要设置很多限制条件,比如限制字符,显示数据类型,限制字符长度等等,其中有很多限制需要通过ASCII码来进行代码编写限制,数不胜数,小编至今还是有很多限制条件的ASCII 不知道,记是永远记不住的,未来用到的时候再多多补充啦。下面是小编在优化学生时经常用到的一些代码限制条件,分享给大家。
Private SubtxtDirector_KeyPress(KeyAscii As Integer)
If KeyAscii > 0 And KeyAscii <> 13And KeyAscii <> 8 Then KeyAscii = 0
End Sub
或者:
Private Sub txtusername_KeyPress(KeyAscii As Integer)
If KeyAscii < 0 Or KeyAscii = 8 Or KeyAscii = 13 Then Exit Sub
KeyAscii = 0
End Sub
或者:
Private Sub txtDirector_KeyPress(KeyAscii As Integer)
If KeyAscii < 0 Or KeyAscii = 8 Then Exit Sub
KeyAscii = 0 ‘不能输入
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Then
Else
KeyAscii = 0
End If
End Sub
If KeyAscii = 8 Then Exit Sub
If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0
If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
End if
If ((KeyAscii <= 57 And KeyAscii >= 48) Or (KeyAscii <= -3652 And KeyAscii >= -20319) Or KeyAscii = 8) = False Then KeyAscii = 0
Private Sub Text1_KeyPress(KeyAscii As Integer)
If ((KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122)) = False Then KeyAscii = 0
End Sub
If KeyAscii <> Asc(".") And (KeyAscii <> 8) And (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) Then
KeyAscii = 0
End If
'添加成绩窗体限制成绩:
(1)限制添加成绩窗体只能输入数字且大于0小于150且可以使用退格键
If KeyAscii = 8 Then Exit Sub
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
ElseIf CLng(txtResult.Text & Chr(KeyAscii)) > 100 Or CLng(txtResult.Text & Chr(KeyAscii)) < 0 Then
KeyAscii = 0
End If
(2)'限制文本框只能输入数字和小数点,但是不可以使用退格键
If KeyAscii < 48 Or KeyAscii > 57 Then
If KeyAscii = 46 Then
If txtResult.Text = "" Or InStr(1, txtResult.Text, ".") <> 0 Then
KeyAscii = 0
KeyAscii = 46
End If
Else
KeyAscii = 0
End If
End If
(3)既限制只输入数字、小数点、又可以限制数值大小,也可以使用退格键
If KeyAscii <> Asc(".") And (KeyAscii <> 8) And (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) Then
KeyAscii = 0
ElseIf CLng(txtResult.Text & Chr(KeyAscii)) > 100 Or CLng(txtResult.Text & Chr(KeyAscii)) < 0 Then
KeyAscii = 0
End If
Private Sub txtName_Change()
txtName.MaxLength = 10 '限制长度为10
End Sub
Private Sub txtName_KeyPress(KeyAscii As Integer)
If ((KeyAscii <= -3652 And KeyAscii >= -20319) Or (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Or KeyAscii = 32 Or KeyAscii = 8) = False Then
KeyAscii = 0
End If
End Sub
Private Sub txtUserName_KeyPress(KeyAscii 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
End Sub
'限制了用户名只能输入数字,大小写字母和删除键,其他输入均被视为无效输入。
Private Sub txtCoursename_KeyPress(KeyAscii As Integer)
If KeyAscii < 0 Or KeyAscii = 8 Or KeyAscii = 13 Then
ElseIf Not Chr(KeyAscii) Like "[a-zA-Z]" Then
KeyAscii = 0
End If
End Sub
Private SubtxtClassroom_KeyPress(KeyAscii As Integer)
If ((KeyAscii >= 48 And KeyAscii <=57) Or (KeyAscii >= 65 And KeyAscii <= 90) Or _
(KeyAscii >= 97 And KeyAscii <=122) Or (KeyAscii = 8)) = flase Then KeyAscii = 0
End Sub
Dim borndate As Date
Dim getdate As Date'定义变量
borndate =Trim(txtBorndate.Text)
getdate =Trim(txtRudate.Text)
If getdate<=borndate then'进行比较
MsgBox"入学时间不能早于出生时间,请重新输入",vbOKOnly + vbInformation,"警告"
txtRudate.SetFocus
Exit Sub
End If
txtClassno.MaxLength = 10
If Val(txtClassno.Text) > 2147483647 Or Val(txtClassno.Text) < 1Then
MsgBox "输入数值在1到2147483647范围内"
txtClassno.SetFocus
Exit Sub
End If
以上内容是小编站在巨人的肩膀上学习和收获到的,感谢走在前边的大佬们,默默无言的给我提供了太多太多的帮助,让我可以走的更加的稳重,善于学习和应用并且不断总结和整理,把知识慢慢积累起来,一点一点的成长吧!