Link to home
Start Free TrialLog in
Avatar of enric
enricFlag for Australia

asked on

Urgent: HandleKeyUp

I'm facing a problem when I press the 'ESC' key when the message box is shown, the form will be unload instead of unloading the messagebox. The 'ESC' key is used as the shortcut key for user to unload the form. I understand that messagebox with vbOkOnly  as VbMsgBoxStyle will unload if any key was press by the user. Therefore, when the user press 'ESC' key, VB identify it as a key to unload the form as well as the messagebox.

Is there any better solution to control that if ESC key is pressed when there is a messagebox, the messagebox should be unload instead of the form. Pls help !

I've included a sample of my code for your better review. Pls take note that the form's property KeyPreview should set to True.


Private Sub Command1_Click()
MsgBox "Hello", vbOKOnly
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim ctr As Control
For Each ctr In Me.Controls
    Select Case TypeName(ctr)
        Case "CommandButton"
            If KeyCode = 27 Then 'Esc key pressed
                Unload Me
            End If
    End Select
Next

End Sub
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of enric

ASKER

Angel, your answer gave me an idea on how to deal with my problem.  Thx!