学生管理系统总结收获——限制字符

一.ASCII码——限制字符问题

在敲学生的时候需要设置很多限制条件,比如限制字符,显示数据类型,限制字符长度等等,其中有很多限制需要通过ASCII码来进行代码编写限制,数不胜数,小编至今还是有很多限制条件的ASCII 不知道,记是永远记不住的,未来用到的时候再多多补充啦。下面是小编在优化学生时经常用到的一些代码限制条件,分享给大家。

1.文本框只能输入汉字:

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

2.只能输入文字及删除键:

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Then
    Else
        KeyAscii = 0
    End If
End Sub

3.限制只能输入数字:

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

4.限制只能输入数字和删除键:

If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) Then
     KeyAscii = 0
 End if

5.只能输入数字和文字:

If ((KeyAscii <= 57 And KeyAscii >= 48) Or (KeyAscii <= -3652 And KeyAscii >= -20319) Or KeyAscii = 8) = False Then KeyAscii = 0

6.只能输入数字和英文字母:

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

7.只输入数字、小数和删除键:

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

8.只能输入文字,英文和空格:

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

9.限制只能输入英文字母、数字以及退格:

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

'限制了用户名只能输入数字,大小写字母和删除键,其他输入均被视为无效输入。

10.限制特殊字符、数字、空格,只能输入汉字和字母


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

11.文本框限制特殊字符不可输入:

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

12.限制出生日期晚于入学日期:

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  

13.限制文本框输入内容的长度

txtClassno.MaxLength = 10

14.限制文本框输入内容的数值范围

If Val(txtClassno.Text) > 2147483647 Or Val(txtClassno.Text) < 1Then
    MsgBox "输入数值在1到2147483647范围内"
    txtClassno.SetFocus
    Exit Sub
End If

以上内容是小编站在巨人的肩膀上学习和收获到的,感谢走在前边的大佬们,默默无言的给我提供了太多太多的帮助,让我可以走的更加的稳重,善于学习和应用并且不断总结和整理,把知识慢慢积累起来,一点一点的成长吧!

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ariel_欢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值