Enable ESC to canel UserForm

G

Guest

I'd like to enable the Escape key to cancel a userform. My form has a single
multipage control with two pages.

I have inserted a commandbutton with its Cancel property set to True and
have hidden the commandbutton by sending it behind the Multipage control.
Its Click event is:

Private Sub CommandButton1_Click()
Unload Me
Set frmInstructions = Nothing
End Sub

I encounter two problems with this: First, the Escape key unloads the form
only when the second page of the multipage is visible (I'd like Esc to cancel
the form at any point). Second, I can't select the button for editing after
it is hidden behind the multipage (need definitive selection method).
 
T

thesquirrel

I recreated this just now, and it works just fine on my end on either
page of the multipage control.

As for selecting the control, you can select your UserForm and in the
drop down in the Properties pane, you will find all the controls on
your form.

theSquirrel
 
T

thesquirrel

I recreated this just now, and it works just fine on my end on either
page of the multipage control.

As for selecting the control, you can select your UserForm and in the
drop down in the Properties pane, you will find all the controls on
your form.

theSquirrel
 
Z

Zone

Jay, I've always put my Cancel button outside of any multi-page area.
Setting the Cancel property to true has always unloaded the form for me
in this situation. So, I would guess, putting the Cancel button on a
page in a multi-page form may circumvent the Esc key functionality, but
that's a guess. Someone else may have an idea. Since it's easy enough
to make the multi-page area of the form smaller than the entire form,
it seems questionable why you would put the form's Cancel button on a
multi-page rather than on the form background, to begin with. James
 
G

Guest

Thanks Squirrel and James -

I keep forgetting how to re-select hidden controls; thanks for the reminder
Squirrel. And, if I put the cancel button behind each page of the multipage,
it works fine.

It just seems like you should be able to drop one button in behind the whole
multipage especially when there is no need for the user to see it. I'll use
your suggestion James and slide a very narrow button in below the multipage
so it just looks like a line.

Thanks for the help !
 
Z

Zone

Jay, I am surprised that just putting the Cancel button on the form
background behind the multipages and setting Cancel to True would not
fire for the Esc key, although I've never tried that. If you think
about it logically, Esc = Cancel, and if the Cancel button is not
visible, it's not available. That does seem to make some sense to me.
What you're really saying by Esc is "close this form and discard
changes". To press the point, maybe the Excel developers thought,
well, if Cancel is not visible, there is no active button for Esc to
refer to. Seems reasonable. And what if the user prefers to use the
mouse instead of a keystroke? Regards, James
 
G

Guest

Hi James-
Thanks for the followup on this one, because although we're close to the
solution, I think there is a simple and routine approach, but I just can't
hit the mark on this one. Maybe I'm missing something.

The issue starts with a desire to make the Escape key dismiss the form. As
far as I can tell, that is not automatic; rather, you must add a
commandbutton to the form, assign 'True' to its Cancel property, assign
'True' to its Enabled property, and 'True' to its Visible property. Then it
functions as desired (Escape fires its click event, which in turn, unloads
the form).

However, if you change its Visible property to 'False' to hide it from the
user, then Escape no longer functions to fire its Click event. So, the
work-around is to leave it Visible, but hide it under other stuff on the form.

The problem I haven't yet solved is that when I try to push it under a
multipage control, it kind of gets 'stuck' in between the pages of the
multipage (sort of). I can't see it when the form runs, but Escape only
unloads the form when Page1 of the multipage is showing (Page0 is the first
page).

I'll post as new information is discovered...
 
G

Guest

Hold the presses, James. The answer was in your first response. Put the
button outside of the multipage control. In fact, don't bother hiding behind
any controls. It just gets 'sticky' and there is some unpredictable
interaction with the multipage control.

The solution to making a 'visible' button 'invisibile' - while still
allowing it to fire a click event when ESC is pushed - lies in first
oversizing the form grid during the design stage. You can put the Cancel
button at the bottom of the grid (or a healthy distance below the controls
you want the user to see). Then, drag the lower edge of the form up so that
the button is outside the lower boundary of what he user will see. The
button remains functional and the user can't see it. Then the user has the
option of using the mouse or pressing escape to cancel the form.

Thanks for your help. Hope to solve another one with you in the future.
 
P

Peter T

The problem I haven't yet solved is that when I try to push it under a
multipage control, it kind of gets 'stuck' in between the pages of the
multipage (sort of). I can't see it when the form runs, but Escape only
unloads the form when Page1 of the multipage is showing (Page0 is the first
page).

Don't put it on the Multipage just put it directly on the form. Set its Left
or Top property way to the right or below the form.
Include 'Unload me' in its click event. Also of course Cancel = true.
You could clean up if necessary in the QueryClose event.

Regards,
Peter T

Jay said:
Hi James-
Thanks for the followup on this one, because although we're close to the
solution, I think there is a simple and routine approach, but I just can't
hit the mark on this one. Maybe I'm missing something.

The issue starts with a desire to make the Escape key dismiss the form. As
far as I can tell, that is not automatic; rather, you must add a
commandbutton to the form, assign 'True' to its Cancel property, assign
'True' to its Enabled property, and 'True' to its Visible property. Then it
functions as desired (Escape fires its click event, which in turn, unloads
the form).

However, if you change its Visible property to 'False' to hide it from the
user, then Escape no longer functions to fire its Click event. So, the
work-around is to leave it Visible, but hide it under other stuff on the form.

The problem I haven't yet solved is that when I try to push it under a
multipage control, it kind of gets 'stuck' in between the pages of the
multipage (sort of). I can't see it when the form runs, but Escape only
unloads the form when Page1 of the multipage is showing (Page0 is the first
page).

I'll post as new information is discovered...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top