Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

GridHitTest

This is a very cool method, added in VFP 6. It lets you figure out where you are in a grid. It is a little strange to use, though, because unlike any other method we can think of, it expects some parameters to be passed by reference.

Usage

lInGrid = grdGrid.GridHitTest( nXCoord, nYCoord
                  [, @nGridComponent [, @nRelativeRow
                  [, @nRelativeColumn [, @nGridPane ] ] ] ] )

Parameter

Value

Meaning

nXCoord, nYCoord

Numeric

The point to check to determine whether it's in the grid.

nGridComponent

Numeric

If the point is in the grid, receives a value indicating which part of the grid that point is in. Otherwise, receives 0.

nRelativeRow

Numeric

If the point is in the grid, receives the relative row in the grid where the point is found.

nRelativeColumn

Numeric

If the point is in the grid, receives the relative column in the grid where the point is found.

nGridPane

Numeric

If the point is in the grid, receives the pane of the grid where the point is found.

lInGrid

.T.

The point is in the grid. "In the grid" includes everything up to the very outside border of the grid.

.F.

The point is not in the grid.

At any time, only one row of a grid is truly active. But there are times when we want to act upon cells other than those in the active row. GridHitTest makes this task much easier. It lets us pass a point and find out not only whether that point is in the grid’s space (which we could do with a little arithmetic), but what part of the grid it’s over. If the point is within the rows and columns, GridHitTest tells us which row and column the point is in. The row and column returned are relative, not absolute. That is, only visible rows and columns are counted. Last and probably least, this method also tells us which pane of the grid the point is in. This is relevant only when the grid is split, something we recommend only rarely.

As we noted above, the format of this method is unique. It’s the only method we’ve encountered that really wants to be a procedure rather than a function. Be sure to precede the last four parameters with “@” so they can receive the appropriate return values.

Help suggests using GridHitTest for OLE Drag and Drop. In fact, it also helps with conventional drag and drop. Dragging and dropping into grid cells has always been a real pain, but this method makes it easy. When the grid’s DragDrop method fires, use GridHitTest to determine whether you’re over a cell and, if so, which one. Then, activate the cell and stuff the data into it. The example shows one way to do it.

Example

* Code for grid's DragDrop event to put data into cell
* There are some assumptions here, including that the control
* of interest is the second one. (The first is always the
* header.) More code could get rid of that assumption.
LPARAMETERS oSource, nXCoord, nYCoord

cData = oSource.Value

LOCAL nComp, nRow, nCol
IF This.GridHitTest(nXCoord,nYCoord,@nComp,@nRow,@nCol)
   IF nComp = 3
      This.SetFocus()
      This.ActivateCell(nRow,nCol)
      This.Columns[This.ActiveColumn].Controls[2].Value = cData
   ENDIF
ENDIF

See Also

ActivateCell, DragDrop, Grid, MCol(), MRow(), OLEDragDrop