Idle Musings of a Delphi Junkie

Formatting numeric attributes in ECO IV

Let me start off by saying that the contents of this blog post is derived from a technique shown to me today by Jose Maria Sanmartin. Full credit should go to him for providing the meat in this particular sandwich.

Normally when data-binding to ECO attributes in VCL.NET there doesn’t appear to be any opportunity to format the output. This is less than ideal if you are displaying things such as currency data to the user. In a standard VCL database application, you could use the DisplayFormat and EditFormat properties of the relevant TField descendant to control this. While these properties are not surfaced at design-time by the ECO handle components, it is still possible to set them. Here is some code I use in my application which allows me to do this :-

procedure ContractManagerEcoSpace.FormatNumericField(AHandle: TElementHandle;
  const AFieldName, ADisplayFormat: string; AEditFormat: string = '');
var
  Field: TADONETNumericField;
begin
  Field := AHandle.FieldByName(AFieldName) as TADONETNumericField;
  Field.DisplayFormat := ADisplayFormat;
  Field.EditFormat := AEditFormat;
end;

Because all ECO handle components ultimately descend from TElementHandle, the above routine can be used with TExpressionHandle, TReferenceHandle and TOclPSHandle references. And here is code demonstrating how to consume this method :-

procedure TInvoiceForm.SetDisplayFormats;
const
  cnCurrencyFormat = '$###,###,##0.00';
begin
  EcoSpace.FormatNumericField(rhCurrentInvoice, 'BillableAmountExTax', cnCurrencyFormat);
  EcoSpace.FormatNumericField(rhCurrentInvoice, 'BillableAmountInclTax', cnCurrencyFormat);
  EcoSpace.FormatNumericField(rhCurrentInvoice, 'GSTAmount', cnCurrencyFormat);

  EcoSpace.FormatNumericField(ehWorkItems, 'BillableAmountExTax', cnCurrencyFormat);
  EcoSpace.FormatNumericField(ehWorkItems, 'BillableAmountInclTax', cnCurrencyFormat);
  EcoSpace.FormatNumericField(ehWorkItems, 'GSTAmount', cnCurrencyFormat);
end;

Once again, thanks to Jose Maria Sanmartin for bringing this technique to my attention.

Posted by David Clegg on September 23rd, 2007 under .NET, ECO |


Leave a Comment



Server Response from: blog2.codegear.com