lowellnelson


I am running a form with a grid. I want KeyPress (from the grid properties) to release the form on ESC; ie
IF nKeyCode = 27
thisform.release
ENDIF
I can assign other keystrokes to KeyPress, and accomplish the task, but nKeyCode = 27 does not respond. What am I missing



Re: KeyPress ESC (neophyte "programmer")

MarciaAkins


I am running a form with a grid. I want KeyPress (from the grid properties) to release the form on ESC

The following code works just fine for me.

Did you remember to set the form's KeyPreview property to .T.

LPARAMETERS nKeyCode, nShiftAltCtrl

IF nKeyCode = 27

Thisform.Release()

NODEFAULT

ENDIF







Re: KeyPress ESC (neophyte "programmer")

Naomi Nosonovsky

In addition to Marcia's reply if you have Close button on the Form set its Cancel property to true. Then pressing ESC will release the form (execute close button's code).




Re: KeyPress ESC (neophyte "programmer")

AndyKr

>> In addition to Marcia's reply if you have Close button on the Form set its Cancel property to true. Then pressing ESC will release the form (execute close button's code).

One additional point on this suggestion. While it is true you need to be careful about the use of VALID clause code. If you have code in the Valid of a control that prevents the control from losing focus, then even with the Cancel property set to .T. it will not work. All such code needs to be bracketed with a test for the ESC key similar to the one Marcia showed:

Code Snippet

IF LASTKEY() = 27

RETURN 1

ELSE

*** Run normal Validation code here

ENDIF






Re: KeyPress ESC (neophyte "programmer")

Naomi Nosonovsky

Here is CancelValidation method I have in my base form class

********************************************************************
* Description.......: CancelValidation
* Calling Samples...:
* Parameter List....:
* Created by........: ideas by John Koziol /Cetin Basoz
* Modified by.......: NN
********************************************************************
local llReturn, loObject
local array laMouseObj[1]

llReturn = lastkey() = 46 && Alt+C - shortcut for Cancel button

if not m.llReturn
	amouseobj(laMouseObj,1) && object under mouse
	loObject = laMouseObj[1,1]

	if vartype(m.loObject) = "O"
		if not thisform.lValidateOnClose
			llReturn = thisform.releasetype = 1 or ;
				(inlist(lower(justext(sys(1272, m.loObject))),"cmdcancel", "cmdclose") ;
				and mdown()) && We're checking for Release type and Close button
		else
			llReturn = lower(justext(sys(1272, m.loObject))) = "cmdcancel" ;
				and mdown()
		endif
	else
		llReturn = .f.
	endif
endif

return m.llReturn

Now, in the textbox's valid I put:

if not thisform.CancelValidation()
* Continue with the valid logic
endif

Therefore if a user clicks on Cancel button or tries to close the form using upper right x button, he/she can do it.





Re: KeyPress ESC (neophyte "programmer")

lowellnelson

Thanks Marcia

If I understand the KeyPreview property correctly, it should be set to .F. if the KeyPress event is to be processed by a control rather than by the form. If the KeyPreview property is .T. then the KeyPress event is intercepted by the form before it goes to the control.

If I use the grid's KeyPress event then ESC still does not work for me, even with the addition of the nodefault command (and whether or not the forms KeyPreview property is set to .T.).

If I set the form's KeyPreview to .T. and use the form's KeyPress property to execute the event code, then ESC works fine. Although it's not a major practical issue to me at the moment, I'm still a little confused about why the grid's KeyPress property does not capture the ESC event, even with the nodefault command.




Re: KeyPress ESC (neophyte "programmer")

lowellnelson

Yes. Thank you Naomi




Re: KeyPress ESC (neophyte "programmer")

lowellnelson

Thanks Andy




Re: KeyPress ESC (neophyte "programmer")

Naomi Nosonovsky

Because it's grid's column textboxes event which fires in this case, not grid's KeyPress (assuming AllowSelection property of the grid is set to true).

You can either use your own textbox class in the grid (as it's done in some frameworks, say, in MereMortals framework) or use BindEvent() to bind textbox KeyPress to grid's KeyPress. Or simply use Form's KeyPreview = .t.

=====================================

BTW, unrelated note - if you want to thank someone as answered your post, you simply press the "Was this post helpful " button to mark the particular post as an answer. See thread bellow in this forum for discussion.





Re: KeyPress ESC (neophyte "programmer")

dni

If you put as you said:
KeyPreview = .T . and use Marcia's code, it should working fine. The form will release only you use keypress method from grid.






Re: KeyPress ESC (neophyte "programmer")

lowellnelson

OK, I understand what you said about the firing of a textbox event. However, your assumption is .F. The AllowSelection property was set to .F. I recognize that I can accomplish what I need at form level, but I still don't know why ESC is not captured by the grid's KeyPress property.




Re: KeyPress ESC (neophyte "programmer")

Naomi Nosonovsky

So, you're saying that you set AllowCellSelection=.F. and still grid's KeyPress is not captured

What is your VFP version





Re: KeyPress ESC (neophyte "programmer")

ibbm

why do you sometimes reference your local variables with m. and sometimes you don't is there a difference

what is laMouseObj... I'm thinking that it is the mouse position but how are you passing that info to lamouseobj

sorry to be so clueless.





Re: KeyPress ESC (neophyte "programmer")

Naomi Nosonovsky

Hi,

If there could be an ambiguity between a field name and a variable I add m. in order to show it's a variable. Many developers ignore this m. completely since they use Hungarian notation (I also do) and are sure that variable name never will be the same as a field name. However, I developed a habit to use m. long time ago looking at and admiring the code by my former colleague Mike Asherman and I'm not going to break this habit.

In other words, I use m. on the right side of the assignation, e.g.

myLocalVar = m.MyOtherLocalVar (here myOtherLocalVar can also be a field name, so I use m.)

Some developers use m. on both sides for consistency.

If you look at my code carefully you'll notice that I declared laMouseObj as an array. And then I used aMouseObj function to put a object selected with the mouse into an array. Look up this function in Help. This function is very useful for custom builders.





Re: KeyPress ESC (neophyte "programmer")

ibbm

I didn't realize that when you did justext that it would take the extension of the properties. I interpreted it as the file extension ( as in myform.scx) which didn't make sense to me I tested it and the light bulb came on! This is great stuff. Now if I wanted to do the cancelvalidation when any of this command group would be pressed is there another command or would i need to test the inlist for all of my command buttons.