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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrow Keys Moving Focus and Executing the Code

Status
Not open for further replies.

NiceArms

Programmer
May 21, 2009
105
GB
Hi All,

I have a form with a combo box which contains a list of users, also the form contains 2 buttons "Exit" & "Begin".

When I click arrow key "up" focus moves to Exit and the code is executed (this also happens when I press shift tab). The same happens when I press arrow key "down" only focus moves to Begin.

Issue 1
I cannot stop focus moving from the combo box (code in the combo box code below). How do I keep focus on this combo box?

Private Sub User_combo_KeyDown(KeyCode As Integer, Shift As Integer)
User_combo.SetFocus 'set focus is not working!!!!
Select Case KeyCode
Case vbKeyDown
If ser_combo.ListIndex <> ser_combo.ListCount - 1 Then
ser_combo.ListIndex = ser_combo.ListIndex + 1
Else
ser_combo.ListIndex = 0
End If
Case vbKeyUp
If ser_combo.ListIndex <> 0 Then
ser_combo.ListIndex = ser_combo.ListIndex - 1
Else
ser_combo.ListIndex = ser_combo.ListCount - 1
End If
End Select
End Sub


Issue 2
When I tab between the objects on the form the code is being executed the moment focus is placed. How do I prevent this and just cause the focus to move and not have the code executed?

/Nice
 
HI NiceArms I'm having a simlar problem, did you find a solution?
 
Might not be eloquent but it works. Set the forms keypreview to true
Code:
Private Sub Combo1_KeyDown(KeyCode As Integer, Shift As Integer)
  lastKey = KeyCode
  Select Case KeyCode
  Case vbKeyDown
   If Combo1.ListIndex <> Combo1.ListCount - 1 Then
        Combo1.ListIndex = Combo1.ListIndex + 1
      Else
        Combo1.ListIndex = 0
      End If
   Case vbKeyUp
      If Combo1.ListIndex <> 0 Then
        Combo1.ListIndex = Combo1.ListIndex - 1
      Else
        Combo1.ListIndex = Combo1.ListCount - 1
      End If
   End Select
End Sub


Private Sub Command0_GotFocus()
  'push focus back
  If returnFocus Then
    Exit Sub
  Else
    MsgBox "gotfocus"
  End If
End Sub

Private Sub Command3_GotFocus()
  'push focus back
  If returnFocus Then
    Exit Sub
  Else
    MsgBox "gotfocus"
  End If
End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  lastKey = KeyCode
End Sub

Public Function returnFocus() As Boolean
  If (Screen.PreviousControl Is Me.Combo1) And (lastKey = vbKeyDown Or lastKey = vbKeyUp) Then
    Me.Combo1.SetFocus
    returnFocus = True
  End If
End Function
 
at the top of the module add

public lastKey as integer
 
add this error check.

Public Function returnFocus() As Boolean
On Error GoTo errlbl
If (Screen.PreviousControl Is Me.Combo1) And (lastKey = vbKeyDown Or lastKey = vbKeyUp) Then
Me.Combo1.SetFocus
returnFocus = True
End If
Exit Function
errlbl:
If Not Err.Number = 2483 Then
MsgBox Err.Number & Err.Description
End If
End Function
 
and add the last line of code

Public Function returnFocus() As Boolean
On Error GoTo errlbl
If (Screen.PreviousControl Is Me.Combo1) And (lastKey = vbKeyDown Or lastKey = vbKeyUp) Then
Me.Combo1.SetFocus
returnFocus = True
lastKey = 0

This allows you to mouse out of the combo
 
Hi MajP,

Apologies for not replying sooner.

I appreciate your responses and I will look into them, unfortunately this project is currently on the "Back Burner" but rest assured I will be coming back to it.

/Nice
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top