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

Trap keypress in container in a grid

Status
Not open for further replies.

AlastairP

Technical User
Feb 8, 2011
286
AU
I have a grid with one column
Column has a container
How do I trap a keypress event, specifically the up/down arrows

I have experimented with a textbox which skips the cursor and refresh the grid, but I have not been able to get the textbox in the container to get focus after each keypress
If I can do it without having the dummy "Keypress" text box, would be better.
 
Don't try to trap a keypress specifically in the grid. If you don't have a control having focus in the container within the column it's quite useless to check for that within the grid.

There is one easy way to trap any keys in the form.keypress event, just set form.keypreview.

form.activecontrol will tell you if the grid or any control within it has focus. And then you may react to a combination of the right keycode for up/down arrow and the right activecontrol.

Bye, Olaf.
 
Thanks, here is the working solution
Note: because all my grids are in a container and are called "Grid1" in the container, I had to use the parent.parent to reference the name of the container, otherwise the reference to the name of the grid would always be .t.

Code:
lcGridname=thisform.oMainform.oTasks.cont2.oOutlookMessageGrid_Single.name

IF TYPE("_Screen.ActiveForm.ActiveControl")=="O"

	LOCAL loActiveControl

	m.loActiveControl = _SCREEN.ACTIVEFORM.ACTIVECONTROL

	IF m.loActiveControl.BASECLASS = "Grid"
	    	lcname=m.loActiveControl.name
	    	TRY 
	    		IF m.loActiveControl.parent.parent.name = lcGridname
				thisform.oMainform.oTasks.cont2.oOutlookMessageGrid_Single.skiprow(nKeyCode)
			ENDIF
		CATCH 
			lcTry=.f.
		ENDTRY 		
	ENDIF 
ENDIF
 
Just some minor things:

1. You should perhaps first check nKeyCode, instead of forwarding all keys to the skiprow method.
2. Why _screen.activeform? Isn't the active form thisform? thisform.keypress wouldn't get a keypress even, if it wasn't.
3. your first line could be abbreviated to lcGridname = "oOutlookMessageGrid_Single"
Because: using the full qualified name from thisform down to the gridcontainer you already use the name of each control of the containment hierarchy down to the controls name itself. If you'd change the name of the oOutlookMessageGrid_Single control, your code wouldn't work anymore, as you'd need to change it to lcGridname=thisform.oMainform.oTasks.cont2.onewname.name and so you don't have any reason to read out the name property in code to prevent code changes. Or put in another way: Any name change will need a code change anyway.

This all is about identifying the right control. As you seem to use a class of a grid within a container, you could add a property "controlid" to the class and let it be "=sys(2015)", then it'll get a unique id at runtime you can compare. Another way would be to compare UPPPER(thisform.name+"oMainform.oTasks.cont2.oOutlookMessageGrid_Single") against UPPER(SYS(1272,m.loActiveControl.parent.parent)). This will then compare the full name path. As you know there can be many controls with same name due to the vfp containment hierarchy names only need to be unique within the same container.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top