2
Public Function EnterToTab(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
        SendKeys "{tab}"
        KeyAscii = 0
    End If
End Function

Private Sub txtUserCode_KeyPress(KeyAscii As Integer)
    Call EnterToTab(KeyAscii)
End Sub
  • 此代码属于登录形式。
  • txtUserCode包含存储在数据库中的特定用户的代码。
  • 运行此表单时,当我输入任何数字txtUserCode并按enter它时不会转到下一个文本框,它的 keyascii 变为 49 不等于 13。
  • 按下 也会发生同样的事情tab
4

3 回答 3

2

setFocus使用该方法而不是模拟 a切换到下一个文本字段TAB怎么样?

Private Sub txtUserCode_KeyPress(KeyAscii As Integer)
    If (KeyAscii = vbKeyReturn) Then
        txtNextTextField.setFocus
    End If
End Sub

您还可以使用控件数组(表单中包含的所有文本字段的数组)并增加索引。因此,您可以将此代码用于表单的所有文本字段,而无需编写冗余代码。

return因此,如果用户在文本字段索引 0 中按下,则将焦点设置为 index+1 (=1)。要创建控件数组,请复制您的第一个文本字段并将其粘贴到表单中。VB6 会询问您是否要创建控件数组。如果您单击“是”,它将自动执行。然后您可以使用以下代码:

Private Sub txtField_KeyPress(Index As Integer, KeyAscii As Integer)
    If (KeyAscii = vbKeyReturn) Then
        If ((Index + 1) < txtField.Count) Then
            txtField(Index+1).setFocus
        Else
            MsgBox "Reached end of form!"
        End If
    End If
End Sub
于 2013-04-01T14:19:27.697 回答
2

有一个 KB142816如何使 ENTER 键像 VB 控件的 TAB 键一样移动焦点,其参考实现与您的类似。但。IMO 最重要的部分是免责声明:

您可以使 ENTER 键将焦点移动到具有下一个较高 TabIndex 属性值的控件,就像 TAB 键一样。

但是,使用 ENTER 键移动焦点不遵循推荐的基于 Microsoft Windows 的应用程序设计指南。ENTER 键应该用于处理默认命令或处理输入的信息,而不是移动焦点。

无论如何,您的代码不起作用的原因是一个谜。由于既不Tab也不Enter将焦点从txtUserCode字段移开,我唯一的猜测是这txtUserCode是唯一一个TabStop属性设置为True. 即根本没有其他控件可以将焦点移至。

于 2013-04-01T19:32:15.507 回答
0

MrSnurb 给出的例子是一个好的开始,但它有很多问题,例如,一个控件可能被禁用或不可见(setfocus 会崩溃),你的 controlarray 中的下一个控件并不意味着它也是下一个控件使用选项卡时会获得焦点(您可以根据需要设置选项卡索引)。我已经变出 2 个“简单”例程(和一个额外的功能),您可以使用它们转到表单上的下一个或上一个控件(实际上没有检查它是否与容器上的控件(框架或其他东西)一起使用) ,所以它可能需要额外的检查..

'##############################################################################
'##
'##     Function fnControlCanHaveFocus
'##
'##############################################################################
'A separate routine, because On Error goto doesn't work with this type of
'error in the IDE within a For Each loop, 
'even if you have set 'Only break on unhandled' errors
Private Function fnControlCanHaveFocus(ByRef ctrl As Control) As Boolean
On Error GoTo ErrorHandling
  '--------------------------------------------------------------
  'Check for properties which lets a control get a focus
  'For now also Check TabStop even though the control CAN have focus if this is off
  fnControlCanHaveFocus = (ctrl.TabStop And _
                           ctrl.Enabled And _
                           ctrl.Visible)
  Exit Function
ErrorHandling:
  fnControlCanHaveFocus = False
End Function

'##############################################################################
'##
'##     Sub pSetFocusToNextControl
'##
'##############################################################################
Private Sub pSetFocusToNextControl(ByRef frm As Form)
  Dim ctrl      As Control
  Dim ctrlFirst As Control
  Dim ctrlNext  As Control
  '--------------------------------------------------------------
  'Is there even an active control?
  If Not frm.ActiveControl Is Nothing Then
    '--------------------------------------------------------------
    'Try and find the First and next control which can receive focus
    Set ctrlFirst = Nothing
    Set ctrlNext = Nothing
    For Each ctrl In frm.Controls
      '--------------------------------------------------------------
      'Can this control have focus?
      If fnControlCanHaveFocus(ctrl) And _
         Not ctrl Is frm.ActiveControl Then
        '--------------------------------------------------------------
        'Check for Next control
        If ctrl.TabIndex > frm.ActiveControl.TabIndex Then
          If ctrlNext Is Nothing Then
            Set ctrlNext = ctrl
          ElseIf ctrlNext.TabIndex > ctrl.TabIndex Then
            Set ctrlNext = ctrl
          End If 'ElseIf ctrlNext.TabIndex>ctrl.TabIndex
        End If 'If ctrl.TabIndex>frm.ActiveControl.TabIndex
        '--------------------------------------------------------------
        'Check for first control
        If ctrlFirst Is Nothing Then
          Set ctrlFirst = ctrl
        ElseIf ctrlFirst.TabIndex < ctrl.TabIndex Then
          Set ctrlFirst = ctrl
        End If 'ElseIf ctrlFirst.TabIndex<ctrl.TabIndex
      End If 'If fnControlCanHaveFocus(ctrl) And...
    Next ctrl
    '--------------------------------------------------------------
    'Is there a next control to set focus to?
    If Not ctrlNext Is Nothing Then
      Call ctrlNext.SetFocus
    '--------------------------------------------------------------
    'No next control, but a first control to jump to?
    ElseIf Not ctrlFirst Is Nothing Then
      Call ctrlFirst.SetFocus
    End If 'ElseIf Not ctrlFirst Is Nothing
  End If 'If Not frm.ActiveControl Is Nothing
End Sub


'##############################################################################
'##
'##     Sub pSetFocusToPreviousControl
'##
'##############################################################################
Private Sub pSetFocusToPreviousControl(ByRef frm As Form)
  Dim ctrl          As Control
  Dim ctrlLast      As Control
  Dim ctrlPrevious  As Control
  '--------------------------------------------------------------
  'Is there even an active control?
  If Not frm.ActiveControl Is Nothing Then
    '--------------------------------------------------------------
    'Try and find the Last and previous control which can receive focus
    Set ctrlLast = Nothing
    Set ctrlPrevious = Nothing
    For Each ctrl In frm.Controls
      '--------------------------------------------------------------
      'Can this control have focus?
      If fnControlCanHaveFocus(ctrl) And _
         Not ctrl Is frm.ActiveControl Then
        '--------------------------------------------------------------
        'Check for Previous control
        If ctrl.TabIndex < frm.ActiveControl.TabIndex Then
          If ctrlPrevious Is Nothing Then
            Set ctrlPrevious = ctrl
          ElseIf ctrlPrevious.TabIndex < ctrl.TabIndex Then
            Set ctrlPrevious = ctrl
          End If 'ElseIf ctrlPrevious.TabIndex<ctrl.TabIndex
        End If 'If ctrl.TabIndex<frm.ActiveControl.TabIndex
        '--------------------------------------------------------------
        'Check for Last control
        If ctrlLast Is Nothing Then
          Set ctrlLast = ctrl
        ElseIf ctrlLast.TabIndex > ctrl.TabIndex Then
          Set ctrlLast = ctrl
        End If 'ElseIf ctrlLast.TabIndex>ctrl.TabIndex
      End If 'If fnControlCanHaveFocus(ctrl) And...
    Next ctrl
    '--------------------------------------------------------------
    'Is there a previous control to set focus to?
    If Not ctrlPrevious Is Nothing Then
      Call ctrlPrevious.SetFocus
    '--------------------------------------------------------------
    'No previous control but a Last control to jump to?
    ElseIf Not ctrlLast Is Nothing Then
      Call ctrlLast.SetFocus
    End If 'ElseIf Not ctrlLast Is Nothing
  End If 'If Not frm.ActiveControl Is Nothing
End Sub

例如,您可以像这样使用它:

Private Sub txt_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)

  Select Case KeyCode 
    Case vbKeyDown, _
         vbKeyReturn
      Call pSetFocusToNextControl(Me)
      KeyCode = 0
    Case vbKeyUp
      Call pSetFocusToPreviousControl(Me)
      KeyCode = 0
  End Select
End Sub
于 2015-05-22T12:08:52.120 回答