學生信息管理系統---優化(一)限制字符

優化學生系統,借鑑了很多博主的博客總結,下面是我的一些總結:

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位字符

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章