'类 Option Explicit Private WithEvents myfrm As Form Public Sub Attach(frm As Form) Set myfrm = frm myfrm.KeyPreview = True End SubPrivate Sub myfrm_KeyUp(KeyCode As Integer, Shift As Integer) If KeyCode = 13 Then SendKeys "{Tab}" End Sub '窗体 Option ExplicitPrivate myCLS As New clsPrivate Sub Form_Load() myCLS.Attach MeEnd Sub Private Sub Form_Unload(Cancel As Integer) Set myCLS = Nothing End Sub Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) Dim isNUM As Boolean If KeyCode = 13 Then Exit Sub isNUM = (KeyCode >= 65) And (KeyCode <= 90) '测试密码框的键盘输入是否是英文字母 If isNUM = False Then Beep Text1.Text = "" '如果输入不是英文字母则响铃并且清空密码框内容 MsgBox "非法字符输入!" End If End Sub
'类
Option Explicit
Private WithEvents myfrm As Form
Public Sub Attach(frm As Form)
Set myfrm = frm
myfrm.KeyPreview = True
End SubPrivate Sub myfrm_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then SendKeys "{Tab}"
End Sub
'窗体
Option ExplicitPrivate myCLS As New clsPrivate Sub Form_Load()
myCLS.Attach MeEnd Sub Private Sub Form_Unload(Cancel As Integer)
Set myCLS = Nothing
End Sub Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim isNUM As Boolean
If KeyCode = 13 Then Exit Sub
isNUM = (KeyCode >= 65) And (KeyCode <= 90)
'测试密码框的键盘输入是否是英文字母
If isNUM = False Then
Beep
Text1.Text = ""
'如果输入不是英文字母则响铃并且清空密码框内容
MsgBox "非法字符输入!"
End If
End Sub