Friday, January 1, 2016

Grid class code with SaveSource and RestoreSource in Visual Foxpro

VFP grids, when you requery (unless using a native view) seem to lose their mind and several devs resort to manual hardcode to restore the columns. Never hand-code your grid restores again. Just take the SaveSource and RestoreSource code from this class and implement it into your framework. (Of course, if you're not subclassing and instead using native controls, this is no help to you....and you really should subclass!!) Big thanks to Paul Mrozowski from my short stint in Michigan back in 2005 for this code. Author: Michael J. Babcock / Paul Mrozowski  Freeware 4.1K Last updated: 2015.12.10

**************************************************
*-- Class:        grdbase (c:\dev\fabnet\classes\mjbbase.vcx)
*-- ParentClass:  grid
*-- BaseClass:    grid
*-- Time Stamp:   12/07/14 10:49:04 PM
*
DEFINE CLASS grdbase AS grid


AllowHeaderSizing = .F.
AllowRowSizing = .F.
DeleteMark = .F.
Height = 200
Width = 320
HighlightBackColor = RGB(255,255,40)
HighlightForeColor = RGB(0,0,0)
HighlightStyle = 2
*-- used in SaveSource/RestoreSource
ocolumninfo = .F.
*-- used in the SaveSource/RestoreSource methods
corigrecordsource = .F.
*-- set to .T. to have Init event set column controlsources
lsetcolumns = .F.
*-- in dblclick method, calls thisform.EditRecord if exists
leditondblclick = .T.
*-- XML Metadata for customizable properties
_memberdata = [<VFPData><memberdata name="leditondblclick" display="lEditOnDblClick"/><memberdata name="lregisterasogrid" display="lRegisterAsOGrid"/><memberdata name="ckey" display="cKey"/><memberdata name="restoresource" display="RestoreSource"/><memberdata name="savesource" display="SaveSource"/><memberdata name="corigrecordsource" display="cOrigRecordSource"/><memberdata name="lsetcolumns" display="lSetColumns"/><memberdata name="ocolumninfo" display="oColumnInfo"/></VFPData>]
*-- set to .T. to have grid register itself as thisform.oGrid in grid.Init event.
lregisterasogrid = .T.
*-- active index for grid (mjb 04-13-14)
ckey = ""
Name = "grdbase"


PROCEDURE savesource
LOCAL loColumn as Column

This.oColumnInfo = CREATEOBJECT("Collection")

*** mjb 12/07/2014 - for some reason, columncount was going to zero!?!?
IF this.ColumnCount = 0 THEN 
SET STEP ON 
this.Init()
ENDIF && this.ColumnCount = 0

FOR EACH loColumn IN This.Columns
    This.oColumnInfo.Add(loColumn.ControlSource)
    loColumn.ControlSource = .NULL.  
ENDFOR

This.cOrigRecordSource = This.RecordSource
This.RecordSource = .NULL.
ENDPROC


*-- restore previous column definitions
PROCEDURE restoresource
LOCAL liIndex as Integer, loColumn as Column

IF VARTYPE(This.oColumnInfo) <> "O"
   RETURN
ENDIF

liIndex = 1

This.RecordSource = This.cOrigRecordSource 

FOR EACH loColumn IN This.Columns    
    loColumn.ControlSource = This.oColumnInfo.Item[liIndex]
    liIndex = liIndex + 1 
ENDFOR
ENDPROC


PROCEDURE DblClick
IF this.lEditOnDblClick AND PEMSTATUS(thisform,thisform.cEditMethod,5) AND NOT EMPTY(thisform.cEditMethod) THEN 
LOCAL lcCmd as String
lcCmd = [thisform.] + thisform.cEditMethod + [()]
&lcCmd
ENDIF 
ENDPROC


PROCEDURE Init
LOCAL loException as Exception
TRY 
IF TYPE("oUtils.oSettings.cHighlightForeColor") = "C" AND !EMPTY(oUtils.oSettings.cHighLightForeColor) THEN 
this.HighlightForeColor = EVALUATE(oUtils.oSettings.cHighlightForeColor)
ENDIF
CATCH TO loException
* ignore...just trap quietly...could report to MBSS later if desired
IF _vfp.StartMode = 0 THEN && developer should be alerted when in dev mode
MESSAGEBOX("Problem setting gridhighlightforecolor for " + this.Name,16,"Color setting problem.")
ENDIF 
ENDTRY 
TRY 
IF TYPE("oUtils.oSettings.cHighlightBackColor") = "C" AND !EMPTY(oUtils.oSettings.cHighLightBackColor) THEN 
this.HighlightBackColor = EVALUATE(oUtils.oSettings.cHighlightBackColor)
ENDIF
CATCH TO loException
* ignore...just trap quietly...could report to MBSS later if desired
IF _vfp.StartMode = 0 THEN && developer should be alerted when in dev mode
MESSAGEBOX("Problem setting gridhighlightforecolor for " + this.Name,16,"Color setting problem.")
ENDIF 
ENDTRY 

IF this.lRegisterAsOGrid THEN 
thisform.oGrid = this
IF PEMSTATUS(thisform,"cAlias",5) AND NOT EMPTY(thisform.cAlias) THEN 
this.RecordSource = thisform.cAlias
ENDIF 
ENDIF

IF this.lSetColumns AND NOT EMPTY(this.RecordSource) THEN 
LOCAL loColumn as Column
FOR EACH loColumn IN this.Columns
loColumn.ControlSource = this.RecordSource + "." + loColumn.Name
ENDFOR
ENDIF && this.lSetColumns
ENDPROC


ENDDEFINE
*
*-- EndDefine: grdbase
**************************************************




No comments:

Post a Comment