In the student system, there are many input boxes, and the input content of each input box should not be the same. Some require the input of name, student ID, class, home address, etc. Because of the demand, it is necessary to limit the input content, the input of pure numbers, the input of pure text, etc. This requires us to use the code we learned before or the corresponding properties to limit.
The following are the main methods of restriction:
The limitation of code type, in fact, most of the input is the characters we type with the keyboard, so we can use ASCII code to limit the input content.
' Class number information can only be numbers
Private Sub txtClassno_KeyPress(KeyAscii As Integer) If KeyAscii = 8 Then Exit Sub If KeyAscii < 48 Or KeyAscii > 57 Then KeyAscii = 0 MsgBox "Illegal characters, only numbers!", vbOKCancel, "Prompt" End If If Len(txtClassno) >= 3 Then txtClassno.Text = "" 'The content of the input box is cleared KeyAscii = 0 'The fourth character entered is cleared MsgBox "The number of digits entered is too long", vbOKCancel, "Prompt" End If End Sub 'Do not support input of special symbols Private Sub txtClassroom_KeyPress(KeyAscii As Integer) If (KeyAscii >= 33 And KeyAscii <= 47) Or (KeyAscii >= 58 And KeyAscii <= 64) Or (KeyAscii >= 91 And KeyAscii <= 96) Or (KeyAscii >= 123 And KeyAscii <= 126) Then ( Note: Some of the punctuation characters have rounded and half-width distinctions. For example: full-width "," "." "·") KeyAscii = 0 MsgBox "Special character input is not supported!", vbOKCancel, "Prompt" End If End Sub 'Only enter letters and numbers If KeyAscii = 8 Then Exit Sub If (KeyAscii >= 48 And KeyAscii <= 57) Or (KeyAscii >= 65 And KeyAscii <= 90) Or (KeyAscii >= 97 And KeyAscii <= 122) Then Else KeyAscii = 0 MsgBox "Illegal characters, only letters or numbers!", vbOKCancel, "Prompt" End If If Len(txtold) >= 6 Then txtold.Text = "" 'The content of the input box is cleared KeyAscii = 0 'The fourth character entered is cleared MsgBox "Password entered is too long", vbOKCancel, "Prompt" End If
Input box input limit modification to frmmodifycourseinfo " modify course information " window