如何释放Qreport中动态创建的控件(100分)

  • 主题发起人 主题发起人 zgj_gd
  • 开始时间 开始时间
Z

zgj_gd

Unregistered / Unconfirmed
GUEST, unregistred user!
能否只释放Qreport中动态创建的控件??
我在Qreport的BeforePrint事件中动态创建了几个TQRLabel控件,其Caption的值是表A中符合某条件的值。当改变A的条件后,在预览报表时,其Caption的值会乱掉。这是因为在预览窗口中Close后,原来创建的TQRLabel控件没有释放!!
 
在afterprint事件中写上TQRLabel.free(创建了几个就free几个);
 
如何记下TQRLabel的名字?因为他们是在BeforePrint事件中创建的,又不是全局变量?
 
object Form1: TForm1
Left = 192
Top = 114
Width = 696
Height = 480
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
Scaled = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object QuickRep1: TQuickRep
Left = 160
Top = 152
Width = 794
Height = 1123
Frame.Color = clBlack
Frame.DrawTop = False
Frame.DrawBottom = False
Frame.DrawLeft = False
Frame.DrawRight = False
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -13
Font.Name = 'Arial'
Font.Style = []
Functions.Strings = (
'PAGENUMBER'
'COLUMNNUMBER'
'REPORTTITLE')
Functions.DATA = (
'0'
'0'
'''''')
Options = [FirstPageHeader, LastPageFooter]
Page.Columns = 1
Page.Orientation = poPortrait
Page.PaperSize = A4
Page.Values = (
100.000000000000000000
2970.000000000000000000
100.000000000000000000
2100.000000000000000000
100.000000000000000000
100.000000000000000000
0.000000000000000000)
PrinterSettings.Copies = 1
PrinterSettings.Duplex = False
PrinterSettings.FirstPage = 0
PrinterSettings.LastPage = 0
PrinterSettings.OutputBin = Auto
PrintIfEmpty = True
SnapToGrid = True
Units = MM
Zoom = 100
object PageHeaderBand1: TQRBand
Left = 38
Top = 38
Width = 718
Height = 131
Frame.Color = clBlack
Frame.DrawTop = False
Frame.DrawBottom = False
Frame.DrawLeft = False
Frame.DrawRight = False
AlignToBottom = False
Color = clWhite
ForceNewColumn = False
ForceNewPage = False
Size.Values = (
346.604166666666700000
1899.708333333333000000)
BandType = rbPageHeader
object QRLabel1: TQRLabel
Left = 32
Top = 32
Width = 58
Height = 17
Frame.Color = clBlack
Frame.DrawTop = False
Frame.DrawBottom = False
Frame.DrawLeft = False
Frame.DrawRight = False
Size.Values = (
44.979166666666670000
84.666666666666670000
84.666666666666670000
153.458333333333300000)
Alignment = taLeftJustify
AlignToBand = False
AutoSize = True
AutoStretch = False
Caption = 'QRLabel1'
Color = clWhite
Transparent = False
WordWrap = True
FontSize = 10
end
end
object TitleBand1: TQRBand
Left = 38
Top = 169
Width = 718
Height = 40
Frame.Color = clBlack
Frame.DrawTop = False
Frame.DrawBottom = False
Frame.DrawLeft = False
Frame.DrawRight = False
AlignToBottom = False
Color = clWhite
ForceNewColumn = False
ForceNewPage = False
Size.Values = (
105.833333333333300000
1899.708333333333000000)
BandType = rbTitle
end
end
object Button1: TButton
Left = 32
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object Button2: TButton
Left = 168
Top = 16
Width = 75
Height = 25
Caption = 'Button2'
TabOrder = 2
OnClick = Button2Click
end
end
================================================================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, QuickRpt, QRCtrls, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
QuickRep1: TQuickRep;
PageHeaderBand1: TQRBand;
QRLabel1: TQRLabel;
TitleBand1: TQRBand;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var L:TQRLabel;
i:integer;
begin
for i:=0 to 5do
begin
L:=TQRLabel.Create(Button1);
L.Parent:=TitleBand1;
L.Left:=30*i;
L.Top:=5;
L.Caption:='L_'+inttostr(i);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
QuickRep1.Preview;
end;

procedure TForm1.Button2Click(Sender: TObject);
var i:integer;
begin
for i:=Button1.ComponentCount-1do
wnto 0do
TQRLabel(Button1.Components).Free;
end;

end.
 
后退
顶部