学生管理系统总结收获——限制字符
生活随笔
收集整理的這篇文章主要介紹了
学生管理系统总结收获——限制字符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一.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 SubKeyAscii = 0End Sub或者: Private Sub txtDirector_KeyPress(KeyAscii As Integer) If KeyAscii < 0 Or KeyAscii = 8 Then Exit Sub KeyAscii = 0 ‘不能輸入 End Sub2.只能輸入文字及刪除鍵:
Private Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii >= -20319 And KeyAscii <= -3652 Or KeyAscii = 8 ThenElseKeyAscii = 0End If End Sub3.限制只能輸入數字:
If KeyAscii = 8 Then Exit Sub If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 04.限制只能輸入數字和刪除鍵:
If KeyAscii <> 8 And (KeyAscii < 48 Or KeyAscii > 57) ThenKeyAscii = 0End if5.只能輸入數字和文字:
If ((KeyAscii <= 57 And KeyAscii >= 48) Or (KeyAscii <= -3652 And KeyAscii >= -20319) Or KeyAscii = 8) = False Then KeyAscii = 06.只能輸入數字和英文字母:
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 Sub7.只輸入數字、小數和刪除鍵:
If KeyAscii <> Asc(".") And (KeyAscii <> 8) And (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) ThenKeyAscii = 0End If'添加成績窗體限制成績: (1)限制添加成績窗體只能輸入數字且大于0小于150且可以使用退格鍵 If KeyAscii = 8 Then Exit SubIf KeyAscii < 48 Or KeyAscii > 57 ThenKeyAscii = 0ElseIf CLng(txtResult.Text & Chr(KeyAscii)) > 100 Or CLng(txtResult.Text & Chr(KeyAscii)) < 0 ThenKeyAscii = 0 End If(2)'限制文本框只能輸入數字和小數點,但是不可以使用退格鍵If KeyAscii < 48 Or KeyAscii > 57 ThenIf KeyAscii = 46 ThenIf txtResult.Text = "" Or InStr(1, txtResult.Text, ".") <> 0 ThenKeyAscii = 0KeyAscii = 46End IfElseKeyAscii = 0End IfEnd If(3)既限制只輸入數字、小數點、又可以限制數值大小,也可以使用退格鍵 If KeyAscii <> Asc(".") And (KeyAscii <> 8) And (KeyAscii < Asc("0") Or KeyAscii > Asc("9")) ThenKeyAscii = 0ElseIf CLng(txtResult.Text & Chr(KeyAscii)) > 100 Or CLng(txtResult.Text & Chr(KeyAscii)) < 0 ThenKeyAscii = 0End If8.只能輸入文字,英文和空格:
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 ThenKeyAscii = 0End If End Sub9.限制只能輸入英文字母、數字以及退格:
Private Sub txtUserName_KeyPress(KeyAscii As Integer)Select Case KeyAsciiCase 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 ThenElseIf Not Chr(KeyAscii) Like "[a-zA-Z]" ThenKeyAscii = 0End If End Sub11.文本框限制特殊字符不可輸入:
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 Sub12.限制出生日期晚于入學日期:
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 = 1014.限制文本框輸入內容的數值范圍
If Val(txtClassno.Text) > 2147483647 Or Val(txtClassno.Text) < 1ThenMsgBox "輸入數值在1到2147483647范圍內"txtClassno.SetFocusExit Sub End If以上內容是小編站在巨人的肩膀上學習和收獲到的,感謝走在前邊的大佬們,默默無言的給我提供了太多太多的幫助,讓我可以走的更加的穩重,善于學習和應用并且不斷總結和整理,把知識慢慢積累起來,一點一點的成長吧!
?
?
?
總結
以上是生活随笔為你收集整理的学生管理系统总结收获——限制字符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PS教程第八课:新建文件
- 下一篇: Windows10桌面美化——打造简洁高