The following example creates 20 edit boxes, using FindComponent with the edit box name to access each newly created edit box.procedure TForm1.Button1Click(Sender: TObject);var i: Integer; const NamePrefix = 'MyEdit'; begin for i := 1 to 20 dobegin TEdit.Create(Self).Name := NamePrefix + IntToStr(i); with TEdit(FindComponent(NamePrefix + IntToStr(i))) do begin Left := 10; Top := i * 20; Parent := self; end; end; end;
with VarForm do begin button1.caption:=... button2.caption:=... ... end;
i: Integer;
const
NamePrefix = 'MyEdit';
begin
for i := 1 to 20 dobegin
TEdit.Create(Self).Name := NamePrefix + IntToStr(i);
with TEdit(FindComponent(NamePrefix + IntToStr(i))) do
begin
Left := 10;
Top := i * 20;
Parent := self;
end;
end;
end;
begin
button1.caption:=...
button2.caption:=...
...
end;
TEdit(AForm.FindComponent('edit1')).Text := 'dasdas'; // 用控件名找到控件对象,然后转换类型,并对其属性赋值
TButton(AForm.FindComponent('button1')).Caption := 'dasdas';
这里假定所有的FindComponent都成功。