簡體   English   中英

用戶表單文本框中的箭頭鍵,VBA

[英]Arrow key in a textbox of a userform, VBA

我正在處理一個帶有多個文本框的用戶表單,這些文本框的排列方式就像一個 Excel 圖表。 我已經設置了 TabIndexes,它們與 TAB/shift+TAB 完美配合。 但是,按箭頭鍵的反應並不是我所期望的。 我這樣命名這些文本框:

boxA1 boxB1 boxC1 boxD1
boxA2 boxB2 boxC2 boxD2 ...
boxA3 boxB3 boxC3 boxD3
          :
          :

假設焦點在boxB1。 當按下向下箭頭鍵時,我希望焦點轉到 boxB2,但它會轉到 boxA3 或其他東西。

我試過這個代碼:

Private Sub boxB1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case vbKeyDown
            boxB2.SetFocus
        Case vbKeyUp
            boxA10.SetFocus
    End Select
End Sub

它工作正常,但是,如果我的表單中只有幾個文本框,這是一個很好且清晰的解決方案。 但是我的表單中有大約 70 個文本框,這會使我的代碼變大並且確實重復。 我可以調整任何參數以使其正確嗎? 或者有什么我可以使用的功能,如下所示? 只是 sudo,你知道。

Private Sub name_i_KeyDown(ByVal KeyCode as MSForms.ReturnInteger, ByVal Shift As Integer)
    Select Case KeyCode
        Case vbKeyDown
            Controls("name_" & i+1).SetFocus
        Case vbKeyUp
            Controls("name_" & i-1).SetFocus
    End Select
End Sub

謝謝!

你走在正確的道路上。 在表單模塊中添加以下兩個 UDF:

Sub SetTextBoxFocus(ByVal KeyCode As MSForms.ReturnInteger)
    Dim sPrevControl As String
    Dim sNextControl As String
    Dim sBoxToSet As String
    Dim oC As Control

    ' Cater for TAB key press. Setting it so that it behaves like vbKeyDown. Remove this If condition if not required
    If Asc(KeyCode) = 57 Then
        KeyCode = vbKeyDown
    End If

    ' We only want to check if pressed key was either vbKeyDown or vbKeyUp and the key was pressed on a TextBox
    If TypeName(Me.ActiveControl) = "TextBox" And (KeyCode = vbKeyDown Or KeyCode = vbKeyUp) Then

        With Me.ActiveControl

            ' Lets set both previous and next text box names
            If InStr(1, .Name, "boxD") > 0 Then
                sPrevControl = "boxC" & GetNumFromString(.Name)
                sNextControl = "boxA" & GetNumFromString(.Name) + 1
            ElseIf InStr(1, .Name, "boxA") > 0 Then
                sPrevControl = "boxD" & GetNumFromString(.Name) - 1
                sNextControl = "boxB" & GetNumFromString(.Name)
            Else
                sPrevControl = "box" & Chr(Asc(Mid(.Name, 4, 1)) - 1) & GetNumFromString(.Name)
                sNextControl = "box" & Chr(Asc(Mid(.Name, 4, 1)) + 1) & GetNumFromString(.Name)
            End If

            ' Bases on which key was pressed, lets set the name of the text box to move to
            Select Case KeyCode
                Case vbKeyDown: sBoxToSet = sNextControl
                Case Else: sBoxToSet = sPrevControl
            End Select

            ' Loop through all controls in the form and set the focus to the control if found
            For Each oC In Me.Controls
                If oC.Name = sBoxToSet Then
                    oC.SetFocus
                    Exit For
                End If
            Next

        End With
    End If

End Sub

Function GetNumFromString(ByVal txt As String) As String
    Dim oRe As New RegExp

    With oRe
        .pattern = "\d"
        If .Test(txt) Then GetNumFromString = .Execute(txt)(0)
    End With

End Function

現在,在每個 TextBox 的 KeyDown 中,進行以下調用: SetTextBoxFocus KeyCode 這應該可以解決問題

注意:我將boxD作為最后一個文本框。 您應該將其更改為同一行中的最后一個文本框

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM