Cool_Guy
Cool_Guy

Reputation: 73

How to deal with enter to tab in vb6?

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

Upvotes: 2

Views: 6550

Answers (3)

SuperDre
SuperDre

Reputation: 194

The example MrSnurb gives is a good start, but it's has a lot of problems, for instance, a control could be disabled or not visible (setfocus will crash), the next control in your controlarray doesn't mean it's also the next control which would get focus when using tab (you can set the tabindex whatever you want). I've conjured 2 'simple' routines (and an extra function) which you can use to go to next or previous controls on a form (haven't actually checked if it works with a control on a container (Frame or something)), so it might need extra checks for that..

'##############################################################################
'##
'##     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

And you use it like this for instance:

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

Upvotes: 0

Ilya Kurnosov
Ilya Kurnosov

Reputation: 3220

There is a KB142816 How To Make ENTER Key Move Focus Like TAB Key for VB Controls with a reference implementation similar to yours. But. It's most important part, IMO, is disclaimer:

You can cause the ENTER key to move the focus to the control with the next higher TabIndex property value, as the TAB key does.

However, using the ENTER key to move the focus does not follow recommended Microsoft Windows-based application design guidelines. The ENTER key should be used to process the default command or to process entered information, not to move the focus.

Anyway, the reason your code doesn't work is a mystery. As neither Tab nor Enter moves focus from txtUserCode field, my only guess is that txtUserCode is the only field with TabStop property set to True. I.e. there's simply no other control to move focus to.

Upvotes: 2

MrSnrub
MrSnrub

Reputation: 1183

What about switching to the next text field using the setFocus method instead of simulating a TAB?

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

You could also use a controls array (array of all text fields contained in your form) and increment the index. So you could use this code for all text fields of your form without having to write redundant code.

So if the user presses return in text field index 0, you set the focus to index+1 (=1). To create a controls array, copy your first text field and paste it to the form. VB6 will ask you whether you want to create a controls array. If you click "yes", it will do automatically. Then you can use the following code:

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

Upvotes: 2

Related Questions