Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using the Enter-key 1

Status
Not open for further replies.

Mollethewizard

IS-IT--Management
Nov 18, 2002
93
SE
Is there a way to navigate between Option buttons in a user form by hitting the Enter-key? It’s annoying when you use the Enter-key to move to the next control and then suddenly have to hit the Tab-key to move along.

Greetings from a grey and cold Sweden.
 
Yes.

Create a Routine (sub) on your form that simply shifts the focus from Option Group to the next control on the form. Then call this routine on each On Key Down event for each option.
Code:
Public Sub MoveOnEnter(ByVal KeyPressed as Long, NextControl as string)
If KeyPressed = vbReturnKey Then
   Me.Controls(NextContol).SetFocus
End If
End Sub

Private Sub OptionX_KeyDown(KeyCode As Integer, Shift As Integer)
   MoveOnEnter "NextControlName"
End Sub
Private Sub OptionY_KeyDown(KeyCode As Integer, Shift As Integer)
   MoveOnEnter "NextControlName"
End Sub
...
If you want to actually navigate the Option Group using the Enter Key you can use a variant of this:
Code:
Public Sub NavigateOnEnter(ByVal KeyPressed As Long, ParentControl As String, CurrentValue As Integer, MaxGroupValue As Integer)
If KeyPressed = vbKeyReturn Then
  If Me.Controls(ParentControl).Value <> MaxGroupValue Then
    Me.Controls(ParentControl).Value = CurrentValue + 1
  Else
    Me.Controls(ParentControl).Value = 1
  End If
End If
End Sub

Private Sub OptionX_KeyDown(KeyCode As Integer, Shift As Integer)
   NavigateOnEnter KeyCode, Me.ActiveControl.Name, Me.ActiveControl.Value, 4
End Sub
Private Sub OptionY_KeyDown(KeyCode As Integer, Shift As Integer)
   NavigateOnEnter KeyCode, Me.ActiveControl.Name, Me.ActiveControl.Value, 4
End Sub
This option will trap the user in the Option Group since the Enter Key will no longer cause the focus to shift from the Option Group.

Hope this helps,
CMP


Instant programmer, just add coffee.
 
Thanks CautionMP for your most helpful advice for a solution! You certainly deserve the STAR!

Mollethewizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top