How to use own function?

类别:编程语言 点击:0 评论:0 推荐:

Use TfrReport.OnUserFunction event. Here is simple example:

   procedure TForm1.frReport1UserFunction(const Name: String;
     p1, p2, p3: Variant; var val: Variant);
  begin
    ifAnsiCompareText('SUMTOSTR', Name) = 0then
       val := My_Convertion_Routine(frParser.Calc(p1));
  end;

   After this, you can use SumToStr function in any place of report (in any expression or script).

(ok, but it works only for one TfrReport component. I want to use my function everywhere (in all TfrReport components).

Make OnUserFunction event handler common for all components. If you can't do this, you should create function library:

    type
     TMyFunctionLibrary = class(TfrFunctionLibrary)
     public
       constructor Create; override;
       procedure DoFunction(FNo: Integer; p1, p2, p3: Variant;
         var val: Variant); override;
    end;

   constructor MyFunctionLibrary.Create;
   begin
     inherited Create;
     with List do
     begin
       Add('DATETOSTR');
       Add('SUMTOSTR');
     end;
   end;

   procedure TMyFunctionLibrary.DoFunction(FNo: Integer;
     p1, p2, p3: Variant; var val: Variant);
   begin
     val := 0;
     case FNo of
       0: val := My_DateConvertion_Routine(frParser.Calc(p1));
       1: val := My_SumConvertion_Routine(frParser.Calc(p1));
     end;
   end;

   To register function library, call
   frRegisterFunctionLibrary(TMyFunctionLibrary);
To unregister library, call
   frUnRegisterFunctionLibrary(TMyFunctionLibrary);

(how I can add my function to function list (in expression builder)?

Use frAddFunctionDesc procedure (FR_Class unit):

  frAddFunctionDesc('SUMTOSTR', 'My functions',
    'SUMTOSTR()/Converts number to its verbal presentation.');

   Note: "/" symbol is required! It separates function syntax from its description.
FuncLib is reference to your function library (can be nil if you don't use the function library). When function library is unregistered, all its function will be automatically removed from the function list.

本文地址:http://com.8s8s.com/it/it24365.htm