Discussion Locked

Unload MDI Form by pressing Escape?

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
How to unload/end MDI Form by pressing vbKeyEscape? i dun see any KeyDown or KeyPress event in MDI Form.

5 replies

by prashant_bajaj

Add a child form to your project, keep the visible status to "False" for this form. Load this form in the form load event of MDI Form.
Handle the Keypress even of this from, check for the keyasccii corresponding to vbKeyEscape and issue a Unload Statement.
When any other form in the application will be called, focus will be the new window, hence pressing the Escape key will not fire the events of the invisibile window, check this out, if face any problems, tell me i will send u the sample code !!

by sl-campbell

Drop the following into the declarations setion of the MDI:

Private Declare Function GetAsyncKeyState Lib "User32" (ByVal vKey As Long) As Integer

Add a Timer control to the MDI

In the MDI Load, activate the timer.

In the Timer control, do something like the following (Out of my head so please be patient if it doesn't work the first time):

If Vb.Forms.Count = 1 then
If GetAsyncKeyState(vbKeyEscape) < 0 Then Unload Me

end if

This uses the VB Forms collection - it will only unload the MDI if the MDI is the only form left open. If have any other forms that are always open but hidden, then you may need to change the Count from 1 to X.

Instead of:

If Vb.Forms.Count = 1 then

you could use:
If myMDIForm.ActiveForm Is Nothing Then

This discussion is closed to new replies.