Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Want record to always be last row of grid
Message
From
11/11/2003 11:58:54
 
 
To
11/11/2003 09:19:04
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00848673
Message ID:
00848764
Views:
17
Hetty,

As you have evidently discovered, there is no grid property to which you have access that sets the current row at a particular location in the grid. This seems to me to be a terrible omission, but as much as I have complained about it, there seems to be no movement to add properties to the grid that would enable us to position the active row.

The method below is my solution to positioning the active row in the grid. We use a similar method consistently even in very large and complex grids, so I believe it works in all grids. However, this code has been modified to remove calls to some custom methods, so be sure to test it thoroughly though if you adopt it.

The core of the method makes use of a technique published by Atkins, Kramik and Schummer in MegaFox, a book I recommend for all VFP developers.
*   BaseGrid.m_Position_Active_Row()
*
*   OVERVIEW:   Repositions the active row on the grid to the top of the 
*               grid or to the row specified in the parameter.  Adapted 
*               from Atkins, Kramik and Schummer, "MegaFox", p. 16-17.
*
*   PARAMETERS:   
*
*   tnRow    The offset from the top of the grid specifying the
*            grid row to which to position the active record.
*            Default is zero ( No offset or "top row" ).
*
*            If "C" or "c" is passed, the active row is placed
*            at the horizontal center of the grid
*
*   SYNTAX:  BaseGrid.m_Position_Active_Row()
*               Positions the active row at top of the grid
*
*            BaseGrid.m_Position_Active_Row( 4 )
*               Positions the active row at the 5th row.
*
*            BaseGrid.m_Move_Active_Row( "C" )
*               Positions the active row at the horizontal center of
*               the grid.
*
LPARAMETERS tnRow

LOCAL lnCount, lnFieldHeight, lnOldGridHeight, llOldLockScreen, lnOldSelect, ;
      lnRecNo, lnRow, lnRowCount

lnOldSelect = SELECT()

WITH thisform

   *  See if the recordsource has any records.  If it has no records, just
   *  return.  There is nothing to position.
   SELECT ( this.RecordSource )
      
   COUNT NEXT 1 TO lnCount

   IF lnCount = 0
      RETURN
   ENDIF

   *   Hide all screen changes from the user until the end of this method.
   llOldLockScreen = .LockScreen
   .LockScreen      = .T.

   WITH this

      lnFieldHeight = ;
         .Height - .HeaderHeight - ;
         IIF( INLIST( .ScrollBars, 1, 3 ), SYSMETRIC( 8 ), 0 )
         
      lnRowCount = ;
         INT( lnFieldHeight / .RowHeight )
      
      DO CASE

      CASE EMPTY( tnRow )
         lnRow = 0

      CASE VARTYPE( tnRow ) = "C" .AND. UPPER( tnRow ) = "C"
         *   Position the active row at horizontal center.
         lnRow = ( INT( lnRowCount / 2 ) - 1 )

      CASE VARTYPE( tnRow ) = "N"
         lnRow = tnRow - 1

      ENDCASE

      lnRow = MAX( 0, MIN( lnRow, lnRowCount - 1 ) )

      *   Save the current record point position in the grid
      *   then move the record pointer in the grid until the
      *   pointer points to the topmost displayed record.  
      *   This may or may not be the first record in the recordsource.
      lnRecNo = RECNO( .RecordSource )

      *   Try to skip back lnRow records.
      SKIP ( lnRow * ( -1 ) ) IN ( .RecordSource )
      
      *   If we reached BOF(), go back to the original record
      *   and skip back one record at at time until BOF() is
      *   reached.
      IF BOF( .RecordSource )
      
         GOTO lnRecNo IN ( .RecordSource )
         
         FOR lnI = 1 TO lnRow - 1

            IF BOF( .RecordSource )
               EXIT
            ENDIF

            SKIP -1

         NEXT
      
      ENDIF
        
      *   Capture the height of the grid to be restored below
      lnOldGridHeight = .Height

      *   Reset the grid height so that only one row is "visible".  This
      *   has the effect of moving the active row to the top of the grid.
      *   SYSMETRIC( 8 ) returns the "height of scroll arrows on horizontal scroll bar"
      *   which is also the height of the scroll bar.
      .Height = .HeaderHeight + .RowHeight + ;
         IIF( INLIST( .ScrollBars, 1, 3 ), SYSMETRIC( 8 ), 0 ) + 1
      
      *   Refresh the grid to reset the recordsource.  This puts the
      *   current record in .RecordSource at top row of the grid.      
      .Refresh()

      *   Reset the grid height back to its original height so all of the
      *   rows are once again displayed.
      .Height = lnOldGridHeight

      *   Go back to the record in .RecordSource for the CurrentActiveRow.
      GOTO lnRecNo IN ( .RecordSource )

      *   Setting focus to the grid repaints it so any dynamic coloration is reimposed.
      .SetFocus()

   ENDWITH   && the Grid

   .LockScreen = llOldLockScreen

ENDWITH && the Form

SELECT ( lnOldSelect )

RETURN
Regards,
Jim Edgar
Jurix Data Corporation
jmedgar@yahoo.com

No trees were destroyed in sending this message. However, a large number of electrons were diverted from their ordinary activities and terribly inconvenienced.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform