Kevin,
Thanks for the help. I’ve been trying to figure this one out for a long time. I spent a couple of hours getting your code integrated into my foundation, and things look good.
However, I came across a few minor problems you may want to know about if you’re using this code yourself.
First, since TrueActiveControl() can potentially return .NULL. (this happened to me during testing), we need to check for this condition in WriteBuffer(). A weird thing in VFP (one of many) is that an object variable can have a TYPE() of "O" and at the same time be .NULL.
Also, when a grid is the active control, the ActiveColumn can be zero (no I don’t know how, but I came across this condition too in testing). I’ve taken this into account in TrueActiveControl().
Finally, the TRIM() function, used in WriteBuffer(), takes a character value as a parameter. The code will generate an error if the value of the active control is not a character value.
FYI: My modified code is below.
=========================================================
*!* **********************************************************************
*!* METHOD TrueActiveControl()
*!* **********************************************************************
*!* Description.......: Returns an object reference to the current active
*!* : control, even if the control is contained in a grid.
*!* Calling Samples...: loActiveControl = THISFORM.TrueActiveControl()
*!* Parameter List....:
*!* Returns...........: An object reference to the current active control.
*!* Return Type ......: Object (or .NULL.)
*!* **********************************************************************
LOCAL loControl
IF TYPE("_SCREEN.ActiveForm.ActiveControl") = "O"
*!* Store an object reference to the
*!* real ActiveControl loControl
loControl = _SCREEN.ACTIVEFORM.ACTIVECONTROL
IF UPPER(ALLTRIM(loControl.BASECLASS)) = "GRID" ;
AND loControl.ACTIVECOLUMN > 0
*!* Object reference to the control
*!* in the grid column:
LOCAL lcJunk
lcJunk = "loControl.Columns(loControl.ActiveColumn)." + ;
loControl.COLUMNS(loControl.ACTIVECOLUMN).CURRENTCONTROL
loControl = &lcJunk
ENDIF
ENDIF
RETURN IIF(TYPE("loControl")=="O" ;
AND UPPER(ALLTRIM(loControl.BASECLASS)) != "GRID", loControl, .NULL.)
=========================================================
=========================================================
*!* **********************************************************************
*!* METHOD WriteBuffer()
*!* **********************************************************************
*!* Description.......: Code to save field value to buffer when clicking on
*!* : a toolbar without leaving the field.
*!* Calling Samples...: THISFORM.WriteBuffer()
*!* Parameter List....:
*!* Returns...........: .T. = success, .F. = failure.
*!* Return Type ......: Logical
*!* **********************************************************************
LOCAL llRetval, loActiveControl
llRetval = .T.
loActiveControl = THISFORM.TrueActiveControl()
IF NOT EMPTY(ALIAS()) AND ;
CURSORGETPROP('Buffering') != 1 AND GETFLDSTATE(0) % 2 = 1 ;
AND TYPE("loActiveControl") == "O" AND !ISNULL(loActiveControl)
IF TYPE("loActiveControl.ControlSource") != "U" AND ;
!EMPTY(loActiveControl.CONTROLSOURCE)
IF EVAL(loActiveControl.CONTROLSOURCE) != loActiveControl.VALUE
REPLACE (loActiveControl.CONTROLSOURCE) WITH loActiveControl.VALUE
*!* We rely on the fact that we revert the field's value
*!* before this next line of code.
IF TYPE("loActiveControl.VALUE") == "C"
llRetval = (TRIM(EVAL(loActiveControl.CONTROLSOURCE)) ;
= TRIM(loActiveControl.VALUE))
ELSE
llRetval = (EVAL(loActiveControl.CONTROLSOURCE) ;
= loActiveControl.VALUE)
ENDIF
ENDIF
ENDIF
ENDIF
RETURN llRetval
=========================================================
Thanks again,
Richard Danley
Tri-B Computer Systems
RICHARD_DANLEY@MSN.COM