动态控件的问题--高分(300分)

  • 主题发起人 sword_liu
  • 开始时间
我倒~~~
for i := 0 to [red]ComponentCount[/red]-1
你要知道ComponentCount是一个变量,
当你Free了一个它的Component以后,ComponentCount就变了阿。
你Free了Components以后,原来的Components[i+1]就变成Components了呀,
所以只能用Downto,这点常识一想就想到了阿。
 
创建时将TQRLabel.Parent:=TQRBand,
这样你就不用手动去释放了,它自动随TQRBand一起FREE。
 
to xianjun
我的意思是不在Form12的Create中运行CreateRep.
因为我要在form12中输入报表的标题,这样作的话,就没办法实现了.
 
老大,我现在如果要在按下按钮后产生QuickRep的Title时(Edit里的内容)都为空压。
 
你所动态创建的控件的Parent全是QuickRep1
把你释放代码的for循环改成这样的:
for i:=0 to QuickRep1.componentcount-1 //主要这句
begin
if (Components is TQRLabel) then TQRLabel(Components).Free;
if (Components is TQRDBText) then TQRDBText(Components).Free;
end;
 
form.close
 
对象的关系是树型关系,你看看我的代码!
释放页头的控件
with PageHeader do
begin
for i := 0 to pageHeader.ComponentCount - 1 do
begin
Components.Free;
end;
end;
释放明细栏的控件
with detail do
begin
for i := 0 to detail.ComponentCount - 1 do
begin
Components.Free;
end;
end;
释放页脚的控件
with Footer do
begin
for i := 0 to pageHeader.ComponentCount - 1 do
begin
Components.Free;
end;
end;
释放统计栏的控件
with Summary do
begin
for i := 0 to pageHeader.ComponentCount - 1 do
begin
Components.Free;
end;
end;
 
哈哈,顺序弄错了,当你free一个空间的时候,控件数组的数目就发生了改变。

把释放的顺序到一下就可以了
var
i:Integer;
begin
for i := ComponentCount-1 downto 0 do
begin
if (Components[0] is TQRLabel) then TQRLabel(Components[0]).Free;
if (Components[0] is TQRDBText) then TQRDBText(Components[0]).Free;
end;
end;
你试试,有问题在联系
 
我的写法就是把所有手工创建的控件交给它的Owner,即你的TForm12去Free,这样做的好处
是不用再自己写Free代码,适用于你的TForm12用完就Free的情形, 但如果你的TForm12是
创建好了要多次调用CreateReport; 方法的话,这样做就不大好,虽然最后TForm12释放的
时候都能把手工创建的控件Free掉,但如果CreateReport; 要执行很多次的话,可能会有效
率问题。
with TForm12.Create(Application) do
try
CreateReport; //动态创建控件
ShowModal;
finally
Release; //由于你在创建QRLabel等时指定Self为Owner,故在此时直接被Free掉
end;
你也可以写成这样:
var
AForm: TForm12;
begin
AForm := TForm12.Create(Application);
try
AForm.CreateReport;
AForm.ShowModal;
finally
AForm.Release;
end;
end;
释放的问题已解决,问题是
来自:sword_liu, 时间:2002-5-31 8:49:00, ID:1134406 | 编辑
老大,我现在如果要在按下按钮后产生QuickRep的Title时(Edit里的内容)都为空压

 
接受答案了.
 
顶部