如何使得VB的文本框只能输入大小字母

2025-03-01 02:40:43
推荐回答(2个)
回答1:

Private Function RegExpTest(StrText, Pattern)
    Dim regex
    Set regex = CreateObject("VBScript.RegExp")
    regex.Pattern = Pattern
    regex.Global = True
    RegExpTest = regex.Test(StrText)
    Set regex = Nothing
End Function

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii <> 8 And Not RegExpTest(Chr(KeyAscii), "[a-z]|[A-Z]") Then
        KeyAscii = 0
    End If
End Sub

回答2:

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc("A") Or KeyAscii > Asc("Z") And KeyAscii < Asc("a") Or KeyAscii > Asc("z") Then KeyAscii = 0
End Sub