Updating condition in method

I have Areas tab which contain grid with some calculations. That calculations depends from area which is selected. Situation is next: One object can have several areas, and when I open Areas tab, it calculates good but, when in object I change Area from one to another, value in calculations stays from previous. On the other words: it not get updated. I am using this code:


[Control(“TabPage”)]
class TabLineAreaGroup
{
public void pageActivated()
{
PMCContractArea contractArea;
AmountMST sumContractArea;

    super();

    pmcContractLine_ds.readCommonAreas(pmcContractLine);
    h1_h2.realValue(pmcContractLine_ds.h1_h2(pmcContractLine));
    efa.realValue(pmcContractLine_ds.efa(pmcContractLine));
    bfa.realValue(pmcContractLine_ds.bfa(pmcContractLine));
    mfa.realValue(pmcContractLine_ds.mfa(pmcContractLine));
    sumArea.realValue(h1_h2.realValue() + efa.realValue() + bfa.realValue() + mfa.realValue());

    while select AreaSelector, sum(RentalValue)
        from contractArea
        group by AreaSelector
        where contractArea.ContractId == pmcContract.ContractId
            && contractArea.RentalObjectId == pmcContractLine.RentalObjectId
    {
        sumContractArea += contractArea.RentalValue;
        switch (contractArea.AreaSelector)
        {
            case PMEAreaSelector::CommonAreaBuilding :
                contractAreaBFA.realValue(contractArea.RentalValue);
                break;
            case PMEAreaSelector::CommonAreaSection :
                contractAreaEFA.realValue(contractArea.RentalValue);
                break;
            case PMEAreaSelector::PrimaryArea, PMEAreaSelector::SecondaryArea :
                contractAreaH1_H2.realValue(contractArea.RentalValue);
                break;
            case PMEAreaSelector::CommonAreaFixed :
                contractAreaMFA.realValue(contractArea.RentalValue);
                break;
        }
    }
    contractAreaSum.realValue(sumContractArea);
}

}



What I need to add in this code, so when area is changed to update the calculations in grid ? Or how to write a event handler for this change ? 

It sounds like you need to run your code when switching records, not in pageActivated(). You can either override active() method of the datasource, or handle its OnActivated event.

Note that you code will be easier to maintain (such as moving logic from one place to another) if you extract the logic to a separate, well-named method. You’ll have to move to a separate method anyway if you find out that you need to execute it from several places.

I would go a step further and move the calculation to a class, instead of having all the logic on a form. It’ll be again easier to maintain and test if it’s not mixed with UI logic and you could potentially use the same logic from several forms.