Page 1 of 1

When Clause behavior of GET

Posted: Wed Aug 22, 2007 11:34 am
by Milan Mehta
Dear All,

I am facing a typical problem regarding When clause of GET. Whenever I am getting focus for a GET, When clause of all the GET of that Dialog gets executed. Is that normal behavior ? If so, how to avoid that ?

My problem is I am recalculating subsequent GETs based on the value of a GET. But I wish to program it such a way that user can override the default calculation done by When clause.

Following is a self containing sample.

TIA

Milan.

-------------------------------------------Cut----------------------------
#include "FiveWin.ch"

#define ETVAR1 101
#define ETVAR2 102
#define ETVAR3 103
#define ETVAR4 104
#define PBOK 105

function Main()

Local oDlg, oVar1, oVar2, oVar3, oVar4
LOCAL nVar1, nVar2, nVar3, nVar4

nVar1 := nVar2 := nVar3 := nVar4 := 0

DEFINE DIALOG oDlg TITLE "Test Update Dialog" RESOURCE "Test"

REDEFINE GET oVar1 VAR nVar1 ID ETVAR1 PICTURE "999999.99" OF oDlg;
VALID {|| nVar2 := nVar1 * 0.125/100, nVar3 := nVar1 * 0.010/100, nVar4 := nVar1 * 0.07/100, .T.}

REDEFINE GET oVar2 VAR nVar2 ID ETVAR2 PICTURE "999999.99" OF oDlg
// WHEN {|| nVar2 := nVar1 * 0.125/100, .T.}

REDEFINE GET oVar3 VAR nVar3 ID ETVAR3 PICTURE "999999.99" OF oDlg WHEN {|| MsgInfo ('When of Var3'), .T.} VALID {|| MsgInfo ('Valid of Var3'), .T.}

REDEFINE GET oVar4 VAR nVar4 ID ETVAR4 PICTURE "999999.99" OF oDlg WHEN {|| MsgInfo ('When of Var4'), .T.}

REDEFINE BUTTON ID PBOK ACTION oDlg:End()

ACTIVATE DIALOG oDlg

return nil
-------------------------------------------Paste--------------------------
RC file.
--------------------------------------------Cut---------------------------
#define ETVAR1 101
#define ETVAR2 102
#define ETVAR3 103
#define ETVAR4 104
#define PBOK 105

TEST DIALOG 6, 15, 260, 167
STYLE WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX
CAPTION "Dialog Update"
FONT 8, "MS Sans Serif"
{
EDITTEXT ETVAR1, 64, 20, 49, 18
EDITTEXT ETVAR2, 64, 43, 48, 18
EDITTEXT ETVAR3, 64, 68, 49, 18
EDITTEXT ETVAR4, 64, 97, 51, 18
PUSHBUTTON "Ok", PBOK, 78, 135, 50, 14
}
-------------------------------------------Paste--------------------------

Posted: Wed Aug 22, 2007 12:01 pm
by Antonio Linares
Milan,

Yes, its a typicall behavior. You can avoid it temporarly changing the bWhen DATA of the GETs:

oGet:bWhen := nil

and later on restoring them

Posted: Wed Aug 22, 2007 4:20 pm
by James Bott
Milan,

I am not clear on what you are trying to accomplish. Your example doesn't match your description. The WHEN clause is usually used to ensure a condition, but you seem to be using it to make a calculation.

Perhaps you should be using bLostFocus. You could calculate the values of Gets 2-4 and place the results in the GETs when the user exits Get1. Then the user would still be free to change the values. Here is an example showing just updating oGet2. You would need to add similar code for oGet3 and oGet4

oGet1:bLostFocus := { || oGet2:varPut( oGet1:varGet() * 0.125/100 ), oGet2:refresh() }

Now when the user enters a value in oGet1 then exits oGet1, oGet2's value is automatically calculated and entered into oGet2, but the user can override it.

There is a complication with this behavior, if the user changes the value in oGet1 after overriding the value in oGet2, then oGet2's value will get recalculated. This may or may not be what you want. If you don't want this behavior, then I would use a flag var to have the calculation occur only on the first exit from oGet1. Something like this:

oGet1:bLostFocus := { || if( lFirst, (oGet2:varPut( oGet1:varGet() * 0.125/100 ), oGet2:refresh()), ) }

James