先设置每个Edit的Tag属性为自己指定的数字, with Form do for i:=0 to ComponentCount-1 do begin if Components[i] is TEdit then case (Components[i] as TEdit ).Tag of {Your stuff code} 1: ;// 2: ;// else // end; end;
试一下这样:for i:= 1 to 10 do begin Tedit(findcomponent('edit' + inttostr(i))).text := inttostr(i); end;
var CtrlName:string; Ctrl:TEdit;for i:=1 to 10 do begin CtrlName:='form.edit'+inttostr(i); Ctrl:=Tlabel(Findcomponent(CtrlName)); Ctrl.text:=....end;
反对for +Case 的嵌套,这样的代码效率和可读性并不强。但tag的使用是一个很好的方向。 大致上while not findcomponent do begin find your edit component having your tag with function findcomponent(), do smoething. end
略作改进: with Form do for i:=0 to ComponentCount-1 do begin if Components[i] is TEdit then IF Components[i].NAME='EDIT1' THEN {DO SOMETHING} ELSE IF Components[i].NAME='EDIT2' THEN .... end;
To tangt(豪鬼):要是对每个Edit的处理都不一样呢?如果是一样的自然也无须Case啦!除了for什么都不用了。To hellen2000(老实和尚):改~~进~~~???:)
for i:=0 to form1.Controlcount-1 do begin if (form1.Controls[i] is TEdit) then (form1.Controls[i] as TEdit).Text:='...'; end;
for i:=0 to ComponentCount-1 do
begin
if Components[i] is TEdit then
case (Components[i] as TEdit ).Tag of
{Your stuff code}
1: ;//
2: ;//
else
//
end;
end;
例如建立一个TImage控件:
TImage *Stanew[100];
for(int w=0;w<100;w++)
Stanew[w]=new TImage(Fb->ScrollBox1);
也许对你有点帮助!
begin
Tedit(findcomponent('edit' + inttostr(i))).text := inttostr(i);
end;
Ctrl:TEdit;for i:=1 to 10 do
begin
CtrlName:='form.edit'+inttostr(i);
Ctrl:=Tlabel(Findcomponent(CtrlName));
Ctrl.text:=....end;
大致上while not findcomponent do
begin
find your edit component having your tag with function findcomponent(),
do smoething.
end
with Form do
for i:=0 to ComponentCount-1 do
begin
if Components[i] is TEdit then
IF Components[i].NAME='EDIT1' THEN
{DO SOMETHING}
ELSE IF Components[i].NAME='EDIT2' THEN
....
end;
To tangt(豪鬼):要是对每个Edit的处理都不一样呢?如果是一样的自然也无须Case啦!除了for什么都不用了。To hellen2000(老实和尚):改~~进~~~???:)
begin
if (form1.Controls[i] is TEdit) then
(form1.Controls[i] as TEdit).Text:='...';
end;