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

How can i cancel a dialog box hiting the "esc"

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I have a dialog box with Ok and a cancel button. But i need to unload the form by hiting the esc key... how can i do it?
 
set the Cancel property of the Cancel button to True
 
...khm .. in a VB project. sorry.
Could you insert the code of your message box to here?
 
You don't seem to understand what i've said...
I have a dialog box open and i want to close it by hiting the "esc" key.
 
Hi,

You could make your own dialogbox, by placing controls on a form and show it modally. Check in Form1_keydown if esc is beeing pressed (keycode = vbKeyEscape (=27)).

Sunaj
 
Sujaj,
I didn't understand very well...
can you write all the code, please!?
I put it on the Form_keydown as:


keycode = vbKeyEscape (=27)

and i can't do it :(


 
yo misunderstood it, you place on the Form_keydown

If keycode = vbKeyEscape (=27) then
Unload Me
End if

it's just that.
 
What a mess!
I still not understanding...
I put it like this:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape = (27) Then
Unload Frm_Dir
End If
End Sub

where is the problem?
 
Hi,

-----------------------------------------------------------
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyEscape Then
Unload me
End If
-----------------------------------------------------------

;-) sunaj

 
How about put the cancel button with the property Cancel at true
It's easier, no?
;)
 
LOL
That's it sunaj, I just made a copy paste from your post to mine of that line and didn't even noticed the (=27).
That was preety obvious though...
 
the codes provided are correct but because some other controls, NOT the form, has the focus - the Form_KeyDown
event will not be triggered. The best solution for you is
to set the Cancel property to TRUE. Else, do this:-

In Form_KeyDown
if keycode = vbkeyEscape then
end
end if


In other controls keydown event call
Form_KeyDown(keycode, shift)

This will synchronize the whole application.

Now, if you have one hundred controls that would be a mess.
Hence, use control array so you can serialize the function
call.

good luck

 
...Or you could set the forms KeyPreview property to true and sunaj's code will work perfectly no matter how many controls you have on your form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top