Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Lastkey() = 27 in combobox
Message
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00368892
Message ID:
00369230
Views:
15
>What kinds of problems have you had with context and LASTKEY()?

A control can loose focus in 2 ways :
1. the user hits a key that sets focus to another control (Enter/TAB/etc ...)
2. the user clicks outside the control

In both cases the valid method will execute BUT in the 2nd case, the value of lastkey() has nothing to do with the way the control was exited and it also may contain 27 (escape) though this wasn't the key used to exit field.

I agree there are solutions to force the lastkey() value as already suggested in this thread, but I simply don't like them. Lastkey() is made to check what was the last key pressed, so use it for that reason !

Here are some additional arguments why I avoid to put all validation in the control's valid routine :
1. the escape issue (already mentioned)
2. if you have a form with several fields to validate, and the user clicks the OK button when he is still on the first control, the valid method of that control is executed and if it returns OK, than what with the validations of the other controls ? They're never executed because the other controls never got the focus ... Conclusion : you have to do some validation for the individual controls when the OK button is pressed, you can't avoid it.
3. It's good you mentioned grid's : try the following : put a validroutine in the valid method of whatever gridelement (for example RETURN NOT EMPTY(THIS.VALUE)). Click on the according field in the grid and leave it blank and click on a control outside the grid : the VALID routine executes and returns false as the field remains blank BUT the field loses focus though it shouldn't : it lost focus with an invalid value !!! Again an occasion where you need exra validation in the OK/SAVE/PRINT/etc ... button !




>> I'm using it in my grid.BeforeRowColChange to handle focus, not for field validation. The disadvantage you describe for your method is serious, I would say. But I haven't really done a lot with complicated field validation, either with .Valid events or with field rules.


>
>The Hacker's Guide has some advice on using LASTKEY() with field validation.
>>Hi,
>>To me, LASTKEY() may but put in the "Things to avoid in VFP" list. You never know if the lastkey() has been used in the context you think it has been. For that reason I NEVER use it ...
>>
>>So how to resolve validation then ?
>>When I validate a field I always accept empty values even there where they're not allowed, but I don't allow to leave a field if it contains an invalid value (other than empty). When the OK/Print/Whatever button is pressed I'm sure that the fields contain a valid value OR are empty. So for those fields that are empty and shouldn't be, I refuse the OK/Print/...
>>
>>Disadvantage : if the user wants to cancel while filling a field, he first needs to empty it or key in a valid value ...
Previous
Reply
Map
View

Click here to load this message in the networking platform