在快速报表VCL(Delphi)中创建和删除对象

时间:2014-09-23 21:25:21

标签: delphi fastreport

我正在使用FastReport 4显示一些动态生成的数据,并在报告中重新排列。

我在报告中使用“模板”对象来获取初始位置(在我的实际程序中我复制字体属性,对齐等)。

我设法创建了一个小项目,因此我可以在报告中创建备忘录组件,预览报告,然后删除组件,以便我可以将报告重用于不同的数据。 / p>

然而,当我释放创建的对象时,我从报告中丢失了其他对象(在这种情况下,我第二次预览报告时找不到我的模板对象。)

从快速报告报告中创建和删除对象的正确方法是什么?

这是pascal单位:

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.

这是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 个答案:

答案 0 :(得分:1)

对不起,我误导了第一个答案 它看起来像PrepareReport中的内部错误,似乎交换了对象。

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;

输出:

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

此处显示的解决方法不是一种可用的方法,因此从文件加载报表或将TfrxReport组件放在将在打印之前创建并在之后销毁的数据模块上可能是更好的解决方法,直到修复此错误。