Cancel, Default

These two button properties let you make things easier for keyboard users by designating cancel (or "escape") and default buttons in a form. When a button is designated as the cancel button, pressing Escape is the same as clicking that button. Pressing Enter is like clicking the default button (unless focus is on another button). These properties correspond to the "\?" and "\!" designators for buttons in FoxPro 2.x.

Usage

cmdButton.Cancel = lIsItCancel
lIsItCancel = cmdButton.Cancel
cmdButton.Default = lIsItDefault
lIsItDefault = cmdButton.Default
There are two strange things associated with these two properties. One is really a Windows thing. That's the relationship between default buttons and the Enter key. Some controls, like edit boxes, accept the Enter key. Unless focus is on one of those controls, pressing Enter chooses the default button. You can specify the default button for any form. But Enter selects the default only when focus isn't on a different command button. As soon as any other button gets focus, pressing Enter chooses that button. When focus moves to a control other than a command button, the button you specified again is the recipient of any Enter.The reason for this weird behavior is that the Enter key is seriously overloaded. Enter is one of the ways to press a button when it has focus. Enter is also the keystroke for choosing the default button. The only way to merge these two behaviors is for a button with focus to be the default button. (FoxPro/DOS solves the problem by requiring Ctrl+Enter to choose the default button.)

The second problem feels like a bug. A form set can have only one cancel button and one default button at a time. But Visual FoxPro isn't consistent about what to do if you mess up and specify more than one. When you change the Default property of any button in a form set to .T., the Default property for every other command button in the form set is set to .F. on the spot. When you set Cancel to .T., any other buttons' Cancel properties don't change. When you run the form, the first button created with Cancel=.T. wins, if it's enabled.


When the user clicks a cancel button, CHR(27), which is ESC, gets put in the keyboard buffer. This makes it possible to test for LASTKEY()=27 in Valid events to see if the user is trying to escape. In that case, you probably want to skip validation of the current field.


And finally, to answer the question you haven't asked yet, yes, a single button can be both the cancel and the default button at the same time.

Example

* Make the OK button be the default and the Cancel
* button be the cancel button
ThisForm.cmdOK.Default=.T.
ThisForm.cmdCancel.Cancel=.T.

See Also

@...Get, CommandButton, Valid


Back to Table of Contents

Copyright © 2002-2018 by Tamar E. Granor, Ted Roche, Doug Hennig, and Della Martin. Click for license .