1
votes

I'm using FastReport 4 to display some dynamically generated data and rearrange it in the report.

I use a "template" object in the report to get the initial position (in my real program I copy font properties, alignment, etc.)

I've managed to create a small project so I can create a memo component in the report, preview the report, and then remove the component so I can reuse the report with different data.

However, when I free the created object, I lose other objects from the report (in this case, my template object is not found the second time I preview the report).

What is the right way to create and remove objects from a Fast Report report?

Here is the pascal unit:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, frxClass;

type
  TForm1 = class(TForm)
    frxReport1: TfrxReport;
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
var
  modelObj: TfrxComponent;
  newObj: TfrxMemoView;
begin
  modelObj := frxReport1.FindObject('modelObj');
  newObj := TfrxMemoView.Create(modelObj.Parent);
  newObj.CreateUniqueName;
  newObj.Text := 'Whee';
  newObj.SetBounds(modelObj.Left, modelObj.Top + modelObj.Height,
    modelObj.Width, modelObj.Height);
  frxReport1.PrepareReport;
  frxReport1.ShowPreparedReport;
  newObj.Free;
end;

end.

Here's the DFM:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 299
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object btn1: TButton
    Left = 224
    Top = 48
    Width = 75
    Height = 25
    Caption = 'btn1'
    TabOrder = 0
    OnClick = btn1Click
  end
  object frxReport1: TfrxReport
    Version = '4.15'
    DotMatrixReport = False
    IniFile = '\Software\Fast Reports'
    PreviewOptions.Buttons = [pbPrint, pbLoad, pbSave, pbExport, pbZoom, pbFind, pbOutline, pbPageSetup, pbTools, pbEdit, pbNavigator, pbExportQuick]
    PreviewOptions.Zoom = 1.000000000000000000
    PrintOptions.Printer = 'Por defecto'
    PrintOptions.PrintOnSheet = 0
    ReportOptions.CreateDate = 41905.757295162040000000
    ReportOptions.LastChange = 41905.757295162040000000
    ScriptLanguage = 'PascalScript'
    ScriptText.Strings = (
      'begin'
      ''
      'end.')
    Left = 72
    Top = 32
    Datasets = <>
    Variables = <>
    Style = <>
    object Data: TfrxDataPage
      Height = 1000.000000000000000000
      Width = 1000.000000000000000000
    end
    object Page1: TfrxReportPage
      PaperWidth = 216.000000000000000000
      PaperHeight = 279.000000000000000000
      PaperSize = 1
      LeftMargin = 10.000000000000000000
      RightMargin = 10.000000000000000000
      TopMargin = 10.000000000000000000
      BottomMargin = 10.000000000000000000
      object PageHeader1: TfrxPageHeader
        Height = 279.685220000000000000
        Top = 18.897650000000000000
        Width = 740.787880000000000000
        object modelObj: TfrxMemoView
          Left = 166.299320000000000000
          Top = 30.236240000000000000
          Width = 264.567100000000000000
          Height = 18.897650000000000000
          ShowHint = False
          Color = clYellow
          Font.Charset = DEFAULT_CHARSET
          Font.Color = clBlack
          Font.Height = -13
          Font.Name = 'Arial'
          Font.Style = []
          Memo.UTF8W = (
            'Model')
          ParentFont = False
        end
      end
    end
  end
end
1

1 Answers

1
votes

Sorry, for my misleading first answer.
It looks like an internal bug in PrepareReport, the objects seem to be exchanged.

var
  modelObj: TfrxComponent;
  newObj: TfrxMemoView;
  cn:String;
begin
  modelObj := frxReport1.FindObject('modelObj');
  newObj := TfrxMemoView.Create(modelObj.Parent);
  newObj.CreateUniqueName;
  cn := newObj.Name; // keep for dirty workaround
  newObj.Text := 'Whee';
  newObj.SetBounds(modelObj.Left, modelObj.Top + modelObj.Height,
    modelObj.Width, modelObj.Height);

  Showmessage('New: ' + newObj.Name + '  modelObj: ' + modelObj.Name);
  frxReport1.PrepareReport;
  Showmessage('New: ' + newObj.Name + '  modelObj: ' + modelObj.Name);

  frxReport1.ShowPreparedReport;
  newObj :=  TfrxMemoView(frxReport1.FindObject(cn)); // dirty workaround
  newObj.Free;
end;

Output:

New: Memo1  modelObj: modelObj
New: modelObj  modelObj: Memo1

The workaround shown here will not be a usable way, so loading reports from a file or placing the TfrxReport component on a datamodule which will be created before printing and destroyed afterwards might be the better workarounds until this bug is fixed.