Const NamePrefix='MyLabel'
.
.
.
for i:=1 to 6 do begin
TfcLabel.Create(Self).Name := NamePrefix + IntToStr(i);
with TfcLabel(FindComponent(NamePrefix + IntToStr(i))) do begin
Parent := Panel1;
Font.Name:='宋体';
Font.Color:=clblue;
Font.Size:=11;
if i=1 then begin
Left:=100;
Top:=200;
end else begin
if (i mod 2)=0 then begin
Left:=TfcLabel(FindComponent(NamePrefix + IntToStr(i-1))).left;
Top:=TfcLabel(FindComponent(NamePrefix + IntToStr(i-1))).top+24;
end else begin
Left:=TfcLabel(FindComponent(NamePrefix + IntToStr(i-1))).left+220;
Top:=TfcLabel(FindComponent(NamePrefix + IntToStr(i-1))).top;
end;
end;
end;
end;
.
.
.
for i:=1 to 6 do begin
TfcLabel.Create(Self).Name := NamePrefix + IntToStr(i);
with TfcLabel(FindComponent(NamePrefix + IntToStr(i))) do begin
Parent := Panel1;
Font.Name:='宋体';
Font.Color:=clblue;
Font.Size:=11;
if i=1 then begin
Left:=100;
Top:=200;
end else begin
if (i mod 2)=0 then begin
Left:=TfcLabel(FindComponent(NamePrefix + IntToStr(i-1))).left;
Top:=TfcLabel(FindComponent(NamePrefix + IntToStr(i-1))).top+24;
end else begin
Left:=TfcLabel(FindComponent(NamePrefix + IntToStr(i-1))).left+220;
Top:=TfcLabel(FindComponent(NamePrefix + IntToStr(i-1))).top;
end;
end;
end;
end;
你创建了一个TfcLabel的实例,却没有任何方式去引用它。Name只是控件的一个属性,并不能产生对控件的引用,动态创建的控件Name为空都可以。这一点与你在IDE中放到Form上的控件不同。Const NamePrefix='MyLabel';
Var
myLabels: array[1..6] of TfcLabel; //因该是全局变量,不能写在某个函数或过程里
//要写在Implementation 前面
//可以直接写在var Form1: TForm; 下面
.
.
.
for i:=1 to 6 do begin
myLabels[i]:=TfcLabel.Create(Self);
with myLabels[i] do begin
Name := NamePrefix + IntToStr(i);
Parent := Panel1;
Font.Name:='宋体';
Font.Color:=clblue;
Font.Size:=11;
if i=1 then begin
Left:=100;
Top:=200;
end else begin
if (i mod 2)=0 then begin
Left:=myLabels[i-1].left;
Top:=myLabels[i-1].top+24;
end else begin
Left:=myLabels[i-1].left+220;
Top:=myLabels[i-1].top;
end;
end;
end; //end of with
end;
让人家怎么帮你找啊!我看可能是
TfcLabel.Create(Self).Name := NamePrefix + IntToStr(i);这里错了!改为
label+inttostr(i) := TfcLabel.Create(Self);
“ DaSaint(齐天大圣)”的答案是对的,
但 “hmzgz81(哩翱) ”朋友找出了错误,但给的答案又是错的,你能这样定义一个变量出来吗?
label是固定,但后缀不定的变量,如果兄台能定义出这样的变量,请告诉我。因为这样我在编程时也能省好多事。
不过动态数组创建了实例,还是希望有释放哦,“有借有还,再借不难”哦