Delphi form

This is a step by step example of creating user defined function with EkUdfList component. On a form EkRTF1 is TEkRTF component. To create your own report function:

1) Place TEkUdfList component on a form. Name it EkUDFList1.

2) Next we need to link UDF list with our EkRTF1 component. For this fill in UDFList property of EkRTF1 component:

3) Select EkUDFList1 component. Open function list in it's context menu or use the same property in the Object inspector:

4) Add new function:

5) The function with number 0 will appear in the list. Set it's name for using in the report template file. For example NewFunction:

6) Set ResultType property. The type of udfrTEkReportVariable means that result variable UDFResult will be an object of TEkReportVariableType. In the function code you will calculate it's value:

7) Set the allowed number of input parametrs for the function. Type 1 for ArgMaxCount property:

8) Use event OnCalculate to create code for your function:

9) Double click and Delphi will create an empty procedure for you. Place the next code here:

procedure TForm1.EkUDFList1Functions0Calculate(Sender: TObject;
Args: TEkUDFArgs; ArgCount: Integer; UDFResult: TObject);
(* ArgCount is number of parameters passed to the function *)
(* Args[] is an array of parameters *)
(* UDFResult - is an object for the function result *)
s := '';

if (ArgCount = 0) then begin
TEkReportVariable(UDFResult).AsString := 'The function was called without parameters';
(* If parameter is a constant or variable *)
if Args[0] is TEkReportVariable then begin
s := TEkReportVariable(Args[0]).AsString;

(* If parameter is a database field *)
if Args[0] is TField then begin
s := TField(Args[0]).AsString;

(* Result *)
TEkReportVariable(UDFResult).AsString := 'The function was called with value: ' + s;

That is all. Now new function may be used in the report template.


Report template

The report template contains three fields with our new user function.


Code to generate the report

procedure TForm1.Button2Click(Sender: TObject);
  (* check report template file *)
  if not FileExists(EkRTF1.InFile) then
    if RTFDialog.Execute then begin
       EkRTF1.InFile := RTFDialog.FileName;
    end else exit;

  (* create report *)
  EkRtf1.CreateVar('Variable1','This is report variable');
  EkRtf1.OutFile := 'EK_RTF_UDF'+IntToStr(Random(10000))+'.doc';