Capture "Enter Key" to initiate command button click in excel vba

huangapple go评论67阅读模式
英文:

Capture "Enter Key" to initiate command button click in excel vba

问题

我有以下代码,用于使用用户窗体输入用户名和密码。当单击命令按钮时,它运行得很好,但我还想捕获当按下"Enter键"时触发命令按钮单击事件。它使用标签标题来确定它处于哪个状态,即用户名或密码,并进行用户名验证以确保它是一个正确的电子邮件地址。我试图使它两种方式都能工作,通过点击命令按钮或按下"Enter键"。现在我必须按两次"Enter键"才能使其工作。

Private Const EMAIL_REGEX As String = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"
Private IsEnteringUsername As Boolean

Private Sub CBUserName_Click()
    Dim regex As Object
    Set regex = CreateObject("VBScript.RegExp")

    regex.Global = True
    regex.IgnoreCase = True
    regex.Pattern = EMAIL_REGEX

    If regex.Test(UsernamePasswordTextBox.Value) Then
        M8Username = UsernamePasswordTextBox.Value
        LbUserPass.Caption = "Please enter your Password"
        CBUserName.Visible = False
        CBPassWord.Visible = True
        UsernamePasswordTextBox.Value = ""
        UsernamePasswordTextBox.PasswordChar = "*"
        UsernamePasswordTextBox.SetFocus
    Else
        MsgBox "Invalid Username!"
        UsernamePasswordTextBox.Value = ""
        UsernamePasswordTextBox.SetFocus
    End If

    Set regex = Nothing
End Sub

Private Sub CBPassWord_Click()
    M8Password = UsernamePasswordTextBox.Value
    LbUserPass.Caption = "Please enter your Username"
    CBUserName.Visible = True
    CBPassWord.Visible = False
    UsernamePasswordTextBox.PasswordChar = ""
    Me.Hide
    GetJobs
End Sub

Private Sub UserForm_Activate()
    UsernamePasswordTextBox.Value = ""
    LbUserPass.Caption = "Please enter your Username"
    UsernamePasswordTextBox.SetFocus
    CBUserName.Visible = True
    CBPassWord.Visible = False
    Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Me.Width)
    Me.Top = Application.Top + (0.5 * Application.Height) - (0.5 * Me.Height)
    IsEnteringUsername = True
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VbQueryClose.vbFormControlMenu Then
        MsgBox "You must enter a valid Username & Password to use this program"
        Cancel = True
    End If
End Sub

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim regex As Object

    If KeyCode = KeyCodeConstants.vbKeyReturn Then
        Set regex = CreateObject("VBScript.RegExp")
        regex.Global = True
        regex.IgnoreCase = True
        regex.Pattern = EMAIL_REGEX

        If IsEnteringUsername Then
            If regex.Test(UsernamePasswordTextBox.Value) Then
                M8Username = UsernamePasswordTextBox.Value
                LbUserPass.Caption = "Please enter your Password"
                CBUserName.Visible = False
                CBPassWord.Visible = True
                UsernamePasswordTextBox.Value = ""
                UsernamePasswordTextBox.PasswordChar = "*"
                UsernamePasswordTextBox.SetFocus
            Else
                MsgBox "Invalid Username!"
                UsernamePasswordTextBox.Value = ""
                UsernamePasswordTextBox.SetFocus
            End If
        Else
            M8Password = UsernamePasswordTextBox.Value
            LbUserPass.Caption = "Please enter your Username"
            CBUserName.Visible = True
            CBPassWord.Visible = False
            UsernamePasswordTextBox.PasswordChar = ""
        End If

        Set regex = Nothing
    End If
End Sub

New code:

Private Sub UsernamePasswordTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
   
    If KeyCode = KeyCodeConstants.vbKeyReturn Then
        If LbUserPass.Caption = "Please enter your Username" Then
            Call CBUserName_Click
        Else
            Call CBPassWord_Click
        End If
    End If

End Sub
英文:

I have the following code to use a userform to input username/password. It works great when the command button is click but I want to also capture when the "Enter Key" is hit to initiate the command button click event. It uses a label caption to determine which state it is in, either username or password and does a username validation to make sure it is a proper email address. I am trying to get it to work both ways, by hitting the command button to initiate or by hitting the "Enter Key". Right now I have to hit the "Enter Key' twice to get it to work.

Private Const EMAIL_REGEX As String = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$"
Private IsEnteringUsername As Boolean

Private Sub CBUserName_Click()
    Dim regex As Object
    Set regex = CreateObject("VBScript.RegExp")

    regex.Global = True
    regex.IgnoreCase = True
    regex.Pattern = EMAIL_REGEX

    If regex.Test(UsernamePasswordTextBox.Value) Then
        M8Username = UsernamePasswordTextBox.Value
        LbUserPass.Caption = "Please enter your Password"
        CBUserName.Visible = False
        CBPassWord.Visible = True
        UsernamePasswordTextBox.Value = ""
        UsernamePasswordTextBox.PasswordChar = "*"
        UsernamePasswordTextBox.SetFocus
    Else
        MsgBox "Invalid Username!"
        UsernamePasswordTextBox.Value = ""
        UsernamePasswordTextBox.SetFocus
    End If

    Set regex = Nothing
End Sub

Private Sub CBPassWord_Click()
    M8Password = UsernamePasswordTextBox.Value
    LbUserPass.Caption = "Please enter your Username"
    CBUserName.Visible = True
    CBPassWord.Visible = False
    UsernamePasswordTextBox.PasswordChar = ""
    Me.Hide
    GetJobs
End Sub

Private Sub UserForm_Activate()
    UsernamePasswordTextBox.Value = ""
    LbUserPass.Caption = "Please enter your Username"
    UsernamePasswordTextBox.SetFocus
    CBUserName.Visible = True
    CBPassWord.Visible = False
    Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Me.Width)
    Me.Top = Application.Top + (0.5 * Application.Height) - (0.5 * Me.Height)
    IsEnteringUsername = True
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
    If CloseMode = VbQueryClose.vbFormControlMenu Then
        MsgBox "You must enter a valid Username & Password to use this program"
        Cancel = True
    End If
End Sub

Private Sub UserForm_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Dim regex As Object

    If KeyCode = KeyCodeConstants.vbKeyReturn Then
        Set regex = CreateObject("VBScript.RegExp")
        regex.Global = True
        regex.IgnoreCase = True
        regex.Pattern = EMAIL_REGEX

        If IsEnteringUsername Then
            If regex.Test(UsernamePasswordTextBox.Value) Then
                M8Username = UsernamePasswordTextBox.Value
                LbUserPass.Caption = "Please enter your Password"
                CBUserName.Visible = False
                CBPassWord.Visible = True
                UsernamePasswordTextBox.Value = ""
                UsernamePasswordTextBox.PasswordChar = "*"
                UsernamePasswordTextBox.SetFocus
            Else
                MsgBox "Invalid Username!"
                UsernamePasswordTextBox.Value = ""
                UsernamePasswordTextBox.SetFocus
            End If
        Else
            M8Password = UsernamePasswordTextBox.Value
            LbUserPass.Caption = "Please enter your Username"
            CBUserName.Visible = True
            CBPassWord.Visible = False
            UsernamePasswordTextBox.PasswordChar = ""
        End If

        Set regex = Nothing
    End If
End Sub

New code:

Private Sub UsernamePasswordTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
   
    If KeyCode = KeyCodeConstants.vbKeyReturn Then
        If LbUserPass.Caption = "Please enter your Username" Then
            Call CBUserName_Click
        Else
            Call CBPassWord_Click
        End If
    End If

End Sub

答案1

得分: 1

Userform_KeyDown如果一个控件获得焦点,将不会触发。相反,在TextBox_KeyDown中捕获Enter键,副作用是TextBox失去焦点。

Private Sub UsernamePasswordTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then
        Call CBUserName_Click
    End If
End Sub
英文:

Userform_KeyDown won't fire if a control has focus. Catch Enter in TextBox_KeyDown instead, the side effect is TextBox losing focus.

Private Sub UsernamePasswordTextBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = 13 Then
        Call CBUserName_Click
    End If
End Sub

huangapple
  • 本文由 发表于 2023年8月9日 00:46:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76861634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定