文本框的字符限制
學生系統中的輸入文本內容,幾乎都會涉及到字符限制問題,需要結合我們的現實生活進行相應的調整。字符限制的類型有多種,將學生系統中遇到的字符限制總結如下,把它們歸結到一起,也會方便我們日后的使用。
1、去文本框空格(即當單擊text就會有空格存在):
2、只能輸入漢字:
Private Sub txt?_KeyPress(KeyAscii As Integer) If KeyAscii < 0 or KeyAscii =13 or KeyAscii = 8 Then Exit SubKeyAscii = 0 End Sub3、限制特殊字符:
A Private Sub txt?_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 SubB Private Sub txt?_KeyPress(KeyAscii As Integer)Dim cTemp As StringcTemp = "`~!@#$%^&*()-=_+[]{};:'\|<>/?.‘“”’、,。——+()《》?,~·……¥!:;【】" & """ '禁止輸入特殊的字符"If InStr(1, cTemp, Chr(KeyAscii)) <> 0 Then KeyAscii = 0 End Sub4、只能輸入字母和漢字:
Private Sub txt?_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 Sub5、只能輸入數字:
Private Sub txt?_KeyPress(KeyAscii As Integer) '只能輸入數字If KeyAscii = 8 ThenElseIf KeyAscii < Asc("0") Or KeyAscii > Asc("9") ThenKeyAscii = 0End IfEnd Sub6、限制粘貼:
If KeyCode = vbKeyV And Shift = vbCtrlMask ThentxtPassword.Enabled = FalseClipboard.CleartxtPassword.Enabled = True End If以上也可用鍵值直接限制,需要什么直接使用對應的鍵值即可。
Private Sub text1_KeyPress(Index As Integer, KeyAscii As Integer)Select Case KeyAsciiCase 48 To 57Case 65 To 90Case 97 To 122Case 8Case ElseKeyAscii = 0End SelectEnd Sub7、文本框彈出提示信息:
比如密碼輸入次數的提示:
總結
- 上一篇: 【学生信息管理系统】——优化篇(一)
- 下一篇: 罗塞塔小结(二)