FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour XBrowse : Calculated Columns
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
XBrowse : Calculated Columns
Posted: Thu Mar 04, 2010 07:11 AM
Similar to Excel, we can also create calculated columns in XBrowse. This is another less known feature of XBrowse.

Here is a small sample. Two calculated columns are created with a simple syntax like :
Code (fw): Select all Collapse
   oBrw:DueDate   := { || oBrw:Date:Value + oBrw:Credit:Value }
   oBrw:Interest  := { || Max( Date() - oBrw:DueDate:Value, 0 ) * oBrw:Advance:Value * 0.1 }

This code is easy to write and easy to understand and maintain. This code has an additional benefit of being portable across different sources of data ( RDD, Array, RecordSet, etc ).

Sample code:
Code (fw): Select all Collapse
#include 'fivewin.ch'
#include 'xbrowse.ch'

function Main()

   local oDlg, oBrw, oFont
   local aData

   SET DATE ITALIAN
   SET CENTURY ON

   aData := CreateRandomData()

   DEFINE FONT oFont NAME 'TAHOMA' SIZE 0,-12
   DEFINE DIALOG oDlg SIZE 500,400 PIXEL FONT oFont ;
      TITLE 'XBrwose Calculated Columns'

   @ 10,10 XBROWSE oBrw SIZE -10,-10 PIXEL OF oDlg ;
      AUTOCOLS ARRAY aData ;
      HEADERS 'Date', 'Advance', 'Credit' ;
      PICTURES nil, '9,999,999.99', '99 Days' ;
      CELL LINES NOBORDER FASTEDIT

   AEval( oBrw:aCols, { |o| o:nEditType := EDIT_GET } )

   // Now add calculated columns

   oBrw:DueDate   := { || oBrw:Date:Value + oBrw:Credit:Value }
   oBrw:Interest  := { || Max( Date() - oBrw:DueDate:Value, 0 ) * oBrw:Advance:Value * 0.1 }

   // end of Calculated columns

   oBrw:DueDate:bClrStd       := { || { If( oBrw:DueDate:Value < Date(), CLR_HRED, CLR_BLACK ), CLR_WHITE } }
   oBrw:Interest:cEditPicture := '@Z 9,999,999.99'
   oBrw:nStretchCol           := 2

   oBrw:CreateFromCode()
   ACTIVATE DIALOG oDlg CENTERED
   RELEASE FONT oFont

return nil

static function CreateRandomData()

   local aData := {}
   local n

   for n := 1 to 50
      AAdd( aData, ;
      {  Date() - HB_RandomInt( 90 ), ;
         Round( HB_Random( 5000, 25000 ), 2 ), ;
         HB_RandomInt( 1, 3 ) * 15 } )
   next n

return aData


ScreenShot:


First three columns can be edited and the calculated columns ( DUEDATE and INTEREST) are recalculated and refreshed automatically.
Regards



G. N. Rao.

Hyderabad, India
Posts: 1335
Joined: Fri Jun 13, 2008 11:04 AM
Re: XBrowse : Calculated Columns
Posted: Thu Mar 04, 2010 07:22 AM

Dear Mr.Rao,

Thank you for providing very useful samples which shows the power of xBrowse. May be many of us here do not know about these powerful features of xBrowse.

Regards
Anser

Continue the discussion