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!

how to define "on key label" in form 1

Status
Not open for further replies.

Sholahudin

Programmer
Feb 7, 2003
4
ID
Hello, i have an Error Message when using "On key label" in form.

ON KEY LABEL "F4" Thisform.Button1.CLICK()

Error Message after Press F4: "THISFORM can only be used within a method."

Please Help
 
0277169

'ON KEY LABEL "F4" Thisform.Button1.CLICK()'

You need on object reference to the form instead of Thisform

You could use

ON KEY LABEL F4 _SCREEN.ActiveForm.Button1.CLICK()

but safer is to create an object reference when you call the form

PUBLIC oForm1
DO FORM form1 NAME oForm1 LINKED

ON KEY LABEL F4 oForm1.Button1.CLICK()

Remember to release the object reference oForm1 when you release the form - an easy way is to put

RELEASE oForm1

in the .UnLoad() event of the form.
FAQ184-2483 - the answer to getting answered.​
Chris [pc2]
 
Hi Sholahudin,

In addition to the good advice Chris has given you, I would advise you to be cautious about using ON KEY LABEL in these circumstances.

ON KEY LABEL (OKL) can fire at any point in your code. If you use it to execute another method or function, you must think carefully about what will happen when you come out of that function. If the function changes the environment in some way, for example by opening another form, the original code might no longer be valid.

Also, you have to make sure that the OKL doesn't get executed in circumstances where it is meaningless. In your case, think what would happen if the user pressed the key when no form was open. Trying to call a method in a non-existent form will cause the code to crash.

For these reasons, you might want to think about re-designing the code to use the Keypress event instead of OKL.
Mike Lewis
Edinburgh, Scotland
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top