【问题标题】:Creating and deleting objects in Fast Report VCL (Delphi)在 Fast Report VCL (Delphi) 中创建和删除对象
【发布时间】:2014-09-23 21:25:21
【问题描述】:

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

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

我已经设法创建了一个小项目,因此我可以在报告中创建一个备忘录组件,预览报告,然后删除该组件,以便我可以重复使用具有不同数据的报告。

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

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

这是帕斯卡单位:

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

【问题讨论】:

    标签: delphi fastreport


    【解决方案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 组件放置在将在打印之前创建并随后销毁的数据模块上可能是更好的解决方法,直到此错误得到修复。

    【讨论】:

    • 啊哈,这是一个错误。作为一种解决方法,我正在创建一个新报告并使用NewReport.AssignAll(BaseReport) 从原始报告中复制。它似乎可以解决问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 2016-03-25
    • 1970-01-01
    相关资源
    最近更新 更多