通过unit1给unit2中的10个edit赋值,编译通过,但是没有结果,一个一个的赋值一点问题没有。
for i := 1 to 10 do
begin
TEdit(findcomponent('form2.edit' + inttostr(i))).text := 'hjkhdja';//这句话能这样写吗?
end;
for i := 1 to 10 do
begin
TEdit(findcomponent('form2.edit' + inttostr(i))).text := 'hjkhdja';//这句话能这样写吗?
end;
findcomponent()里面的参数是个STRING型的。返回值是个TComponent,所以如果你的组件名是form2.edit1,form2.edit2之类,我觉得应该是:
findcomponent('form2.edit' + inttostr(i)).text := 'hjkhdja';
这句话不对,字符串的相加好象不这样.查查吧
var I: Integer;
begin
with Form2 do
begin
for I := 1 to 10 do
TEdit(findcomponent('Edit' + inttostr(i))).text := 'hjkhdja';
end;
Form2.Show;
end;
var
i: Integer;
const
NamePrefix = 'form2.MyEdit';
begin
for i := 1 to 20 do
begin
TEdit.Create(Self).Name := NamePrefix + IntToStr(i);
with TEdit(FindComponent(NamePrefix + IntToStr(i))) do
begin
Left := 10;
Top := i * 20;
Parent := self;
text:=inttostr(i);
end;
end;
end;
d6上的这例子可以。
begin
if Form2.Components[i] is TEdit then
(Form2.Components[i] as TEdit).Text := 'aaaaa';
end;
这样写就OK了