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

ON KEY LABEL problem

Status
Not open for further replies.

LALLORA

Programmer
Aug 6, 2003
22
IT
Hi all.

I've this problem. In the MAIN.PRG I use

ON KEY LABEL F10 xfnClickSave()

where xfnClickSave() is a function which moves the Focus to the SAVE button and performs the Click() method. This allows the user to press F10 to save the data.

Now all works fine but, sometimes, I get this error

Cannot call SetFocus from within a When, Valid, RangeHigh or RangeLow event.

Chiamanti: D:\RADAR\E-RADAR.EXE
PROCEDURE APPLICATION.XPROCDO
PROCEDURE APPTOOLBAR.CMDRICAMBI.CLICK
MENUMOVR.MPX
PROCEDURE APPLICATION.XPROCXDOFORMFORSTARTUP
PROCEDURE APPLICATION.XPROCXDOFORM
PROCEDURE FRMADMAG.QUERYINTERVENTO.TXTEDIT.VALID
PROCEDURE APPLICATION.XPROCRADARRLOCK
PROCEDURE XFNCLICKSAVE FUNCS.FXP

This happens because the XFNCLICKSAVE function, which performs the SetFocus instructions, is called during the FRMADMAG.QUERYINTERVENTO.TXTEDIT.VALID method... but I DO NOT CALL IT IN THE VALID... it happens time to time and I do not know how to avoid it... I'm not able to replicate it.

Is there someone who has already faced this kind of problem ?

It looks like a 'timing' problem... the F10 event starts when the valid is running...

Thanks.
Ciao.


Marco (Italy)
marco.dicesare@elmec.it
 
Further info.

I interviewed the user and he told me that when the cursor is into a field he presses ENTER and F10 in a very very short time.

I told him not to do so as to avoid the error but the problem exists.

I cannot issue a PUSH KEY CLEAR and POP KEY in each VALID method for each field in my application so as to stop the ON KEY during the valid...

Can I know if the program is in a VALID method, I mean if in the "stack" of the method called there is a VALID method ?

Thanks.


Marco (Italy)
marco.dicesare@elmec.it
 
Unfortunatly I must jump out from the field so as to validate/format it before save the data. I could be in a grid field, in a spinner field... everywhere.

Thanks.

Marco (Italy)
marco.dicesare@elmec.it
 
Ok I found the way. In the Save function I added the following code to check if in the program caller stack a VALID method was called :

* Check for all the nidification levels
FOR lnCaller = 1 TO 128
lcProgram = SYS(16, lnCaller)
DO CASE
* The stack pointer points to myself
CASE PROGRAM() $ lcProgram OR "ERRORS" $ lcProgram
EXIT
CASE lcProgram <> &quot;ON...&quot;
* Valid
IF &quot;.VALID &quot; $ (ALLTRIM(UPPER(lcProgram)) + &quot; &quot;)
* I did a PUSH KEY CLEAR
POP KEY
* Do not save
RETURN .F.
ENDIF
ENDCASE
ENDFOR



Marco (Italy)
marco.dicesare@elmec.it
 
Try using the Key PressEvent on a form instead of using ON KEY LABLE, which is difficult to debug.

In order to do this you must (1) first set the property &quot;key preview&quot; on the form to .T.

(2) Then add the code in the KeyPress Event.

Here is an example of one that I use on a text box of a search form to force the cmdOK.button to Click when the user completes presses enter in a text field:

IF LASTKEY() = 13
* Enter KEy
THISFORM.cmdOK.CLICK()
ENDIF
IF LASTKEY() = 27
* Escape KEy
THISFORM.cmdCancel.CLICK()
ENDIF

It sounds like you example needs to have the code in the KeyPress Event of the form, not in a text box like I showed.

Cheers

 
Marco,

This doesn't answer your question, but I would advise you to avoid using ON KEY LABEL whenever possible. The problem is that it does not respect the Foxpro event model -- you cannot control when it will fire, and it is too easy to jump out of a method which is performing some important process.

The KeyPress event is a much better way to go. I would take a look at Jimoo's code (above) and go on from there.

Mike


Mike Lewis
Edinburgh, Scotland
 
I took another look at the INKEY help and noticed the key value for F10 (your example) is -9

Put the following code in the KEYPRESS Event of the form:

IF LASTKEY() = -9
DO Your_Routine
ENDIF

* Remember. You still need to set the KeyPreview Property on the form to .T.


Jim Osieczonek
Delta Business Group, LLC
jimo@deltabg.com
 
One more nit ....

The F10 key is a system-reserved keypress for activating the system menu. (Try it in Word, Excel, etc...)

I'd be very wary of using it for anything non-standard.
 
Thanks for your tek-tips !

I replaced all my ON KEY LABEL with the code in the KeyPress event and it works. I left te F10 ON KEY LABEL because users want to use this key to confirm the input (AS/400 key ?) and F10 is a reserved key that KeyPress is not able to capture... if anyone nows how to trap it...

Ciao.

Marco (Italy)
marco.dicesare@elmec.it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top