在VB中,怎么限制TEXT中的内容只能是数字或者字母,且限制输入的个数。

2024-11-05 23:38:30
推荐回答(3个)
回答(1):

Private Sub Form_Load()
Text1.MaxLength = 10 '限制输入字符数

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 '只能输入数字

Case Else
KeyAscii = 0

End Select
End Sub

回答(2):

在响应事件时判断输入的字符是否符合要求,不符合则删除或返回0

回答(3):

Private Sub Form_Load()
Text1.Text = ""
Text1.MaxLength = 11
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 46 Then Exit Sub
If KeyAscii >= 48 And KeyAscii <= 57 Then Exit Sub
If KeyAscii >= 65 And KeyAscii <= 90 Then Exit Sub
If KeyAscii >= 97 And KeyAscii <= 122 Then Exit Sub
If KeyAscii = 13 Then SendKeys "{Tab}"
KeyAscii = 0
End Sub