for i:=0 to count do
begin
myImage[i]:=TImage.Create(self);
with myImage[i] do
begin
Parent:=Self;
left:=10+i*50;
top:=10+i*50;
height:=80;
width:=72;
tag:=i+1;
end;
end;提示错误在myImage[i]:=TImage.Create(self);
求助!
begin
myImage[i]:=TImage.Create(self);
with myImage[i] do
begin
Parent:=Self;
left:=10+i*50;
top:=10+i*50;
height:=80;
width:=72;
tag:=i+1;
end;
end;提示错误在myImage[i]:=TImage.Create(self);
求助!
myImage[i]:=TImage.Create(nil);//建议使用
myImage[i]:=TImage.Create(form1);
myImage[i]:=TImage.Create(Application);
你对Create(AOwner: TComponent)方法里的AOwner了解不够。
AOwner的意思是“谁负责释放这个组件的内存呢?”
所以这个AOwner可以写Self、Application、也可以是nil。也就是可以这么写:
MyObj.Create(Self); //写这代码所在文件里的本对象释放内存,如果写在Form里那么Form释放
MyObj.Create(Application);//Application对象结束时候释放内存,也就是程序结束释放。
MyObj.Create(nil);//程序作者自己负责释放内存,必须用try...finally
比如:
img:= TImage.Create(nil);
try
img.Picture.LoadFromFile:= 'aa.bmp';
finally
img.free; //must free!
end;如果写了Application、Self当然很省事了,不用自己操心如何释放。当然,Self释放更快,Application则占用内存时间比较长,不推荐。你程序出错是因为你的Image数组根本没写在一个父类为TComponent的子类实现代码中。
所以写成myImage[i]:=TImage.Create(nil);才对,当然,注意释放。