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

I have a grid control on m0y form.

Status
Not open for further replies.

Ravindra Patil

Programmer
Feb 3, 2024
4
IN
I have a grid control on m0y form. When the user presses Enter or Tab in the last column, I want the first cell in next row should get the focus. I tried by inserting following code in last column's text1's LostFocus. But the control remains in that row only.

thisform.addrow(.t.,this.Parent.Parent.RecordSource)
this.Parent.Parent.Refresh
ThisForm.Mytxtbox11.Value=RECNO('_aggre')
ThisForm.Mytxtbox11.Refresh
This.parent.parent.ActivateCell(RECNO('_aggre')+1,1) &&column2.SetFocus()


Code for Form's addrow method is as follows;

PARAMETERS add_row,tbl
if INLIST(LASTKEY(),13,9,24) OR add_row
SELECT sr_no FROM &tbl WITH (buffering=.t.) WHERE INLIST(.t., EMPTY(date), EMPTY(size), EMPTY(agency), EMPTY(AMOUNT)) AND !DELETED() INTO ARRAY a_sn
SELECT &tbl
IF _tally>0
MESSAGEBOX("Sr.No : "+TRANSFORM(a_sn[1])+" -> The fields date, size, quantity, rate, agency are compulsory.",64,"Attention please!")
ELSE
oldsn= sr_no
LOCATE FOR sr_no=oldsn+1
IF !FOUND()
APPEND BLANK
this.justadded=RECNO()
GO this.justadded && purposely inserted to avoid error
REPLACE cont_code WITH contracts.code, sr_no WITH oldsn+1
if ThisForm.Add_mod_del1.isadding
REPLACE date WITH date()
endi
ENDIF
ENDIF
ENDIF
 
Hi Ravindra Patil,

please put the following code into the BeforeRowColChange event of Your grid:

Code:
LPARAMETERS nColIndex

WITH THIS
	IF nColIndex = .ColumnCount
		.ActivateCell( .ActiveRow + 1, 1 )
	ENDIF
ENDWITH

Regards, Stefan
 
ActivateCell is a nice approach, but it will only work in all but the last row of the grid, because it does not add a new record.

Well, regarding your own tried code, Ravindra Patil: Lostfocus is the wrong place, as LostFocus event could only occur, if there would be anything to go next, but VFP sticks to the last cell, if there are no further records, so that code would work, as it isn't even run, if you just would combine your code with the idea to use the BeforeColChange event instead of the Lostfocus event, that may work, but again, the BeforeColChange event happens only, if there is a new cell the text cursor would go to, even though it is called "Before", it does not happen when you just try to move from the last cell with TAB or ENTER, the requirement is a row or column change actually being possible.

There is a much simpler solution though: Set the grids AllowAddNew property to .T., see
Now the only problem you have, is that the new record the grid then automatically adds to the grids recordsource cursor, is not populated by your code, but by default values you define in the table or cursor.

Chriss
 
Ravindra Patil said:
When the user presses Enter or Tab in the last column...

I realize you didn't even mention the last row in that. But do you really want to add a new row before the user is in the last row? So a user can add a record in the last column, no matter what row?

I'd still say AllowAddNew is the simplest solution, although you then will not be able to populate fields with defaults when using free tables, because free tables don't offer that feature of defaults.

Chriss
 
Also, I cleared my uncertainty now and indeed the BeforeRowColChange event happens also in the last row, so that's the universal event to use, also when the user uses TAB or RETURN in the bottom rightmost cell.

There is still a problem in Stefans code aside from not adding a row: If you backtab or use left arrow in the last column, it still activates the first column of the next row instead of going to the previous column, same row. That's because the BeforeRowColChange won't tell where the user wants to go. You can use LASTKEY() to find out, as you do in your own code, Ravindra.

To combine Stefans and your code, this should work:
Code:
LPARAMETERS nColIndex

Local lnTargetRow

WITH This
    IF nColIndex = .ColumnCount And INLIST(LASTKEY(),13,9,24,4) && 4 is for right arrow key
       lnTargetRow = .ActiveRow + 1
       Skip 1
       If Eof()
          Thisform.addrow(.t.,This.RecordSource) && notice: as this is a grid method "This" is the grid.
       Else
          Skip -1
       EndIf
       .ActivateCell( lnTargetRow, 1 )
       This.refresh()
    EndIf
EndWith

And now that also works without using AllowAddNew, indeed using that code better leave AllowAddNew at .F., or you may get two new records (I haven't checked that out, though).

It mainly has the advantage you now can populate a new record with your code and not just with a virtual APPEND BLANK.

Of course, AllowAddNew does most of this without any code, so it's still an alternative. And to mend the problem of AllowAddNew only doing an APPEND BLANK: You could use the insert trigger mechanism of a DBF that is part of a DBC to use code populating a new record aside from default values you can also define for tables of a DBC.

Chriss
 
All that said, you surely find more inspiration for how to handle keys in a grid from a search...

searchresults_tpfurx.jpg


Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top