学生信息管理系统---优化(一)限制字符

优化学生系统,借鉴了很多博主的博客总结,下面是我的一些总结:

1.限制文本框只能输入汉字和删除键

Private Sub txtDirector_KeyPress(KeyAscii As Integer)   
 If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 Then  
   Else    
       KeyAscii = 0     
          MsgBox "请输入汉字!", vbOKOnly + vbExclamation, "提示"      
            txtDirector.SetFocus  
              End If
              End Sub
  

2.文本框只能输入数字和删除键

Private Sub txtClassno_KeyPress(KeyAscii As Integer)   
 Select Case KeyAscii       
  Case 48 To 57     
     Case 8   
      Case Else       
       KeyAscii = 0     
          MsgBox "请输入数字", vbOKOnly + vbExclamation, "提示"       
           txtClassno.Text = ""   
            End Select
            End Sub
 


3.限制输入的号码长度

If Len(txtTel.Text) <> 11 Then  
  MsgBox "请输入11位数字电话号码", vbOKOnly + vbExclamation, "警告"  
    txtTel.Text = ""  
      txtTel.SetFocus  
        Exit Sub
        End If

4.断文本框是否输入数字

If Not IsNumeric(Trim(txtSID.Text)) Then 
   MsgBox "请输入数字!", vbOKOnly + vbExclamation, "警告" 
      Exit Sub   
       txtSID.SetFocus
       End If

5.限制成绩取值范围为0-120

Private Sub txtResult_Change()
On Error Resume NextIf Val(Trim(txtResult.Text)) > 120 Or (Trim(txtResult.Text)) < 0 Then  
  MsgBox "输入数字超出范围,请重新输入"  
    txtResult.Text = ""   
     txtResult.SetFocus
     End If
     End Sub

6.只能输入汉字和数字

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

7.只能输入汉字和英文字母

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)KeyAscii = 32 Or KeyAscii = 8) = False Then   
      KeyAscii = 0 
         End If
         End Sub

8.限制出生日期小于入校日期

If DTPicker1.Value >= DTPicker2.Value Then
        MsgBox "出生日期应该小于入学日期,请重新输入!", vbOKOnly + vbExclamation, "警告"
        DTPicker1.SetFocus
        DTPicker2.SetFocus
    Else

9.限制键盘不能输入内容

KeyAscii = 0  '限制键盘不能输入内容

还有些限制可以在控件属性(MaxLength)中修改,例如电话号码需要限制11位字符

发布了21 篇原创文章 · 获赞 1 · 访问量 1323

猜你喜欢

转载自blog.csdn.net/weixin_44682554/article/details/100096111