2017-10-05 52 views
0

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

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 

謝謝!

+0

您需要一個包含綁定到每個文本框的通用keyDown/up處理程序的類模塊。想知道這是如何工作的,請參閱:http://jkp-ads.com/Articles/ControlHandler00.asp – jkpieterse

回答

1

你走在正確的道路上。添加空兩名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 

,在的KeyDown每個文本框,進行以下電話:SetTextBoxFocus KeyCode。這應該是關鍵

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

相關問題