此代码可操作窗体中多个同类组件,如果对多个TEdit操作用一条循环语句即可完成。代码在D7下测试运行无误。
var
Form1: TForm1;
SArrayEdit: Array [0..3] of TEdit;
DArrayEdit: Array of TEdit;
//
//
procedure TForm1.FormCreate(Sender: TObject);
begin
SArrayEdit[0] := Edit1;
SArrayEdit[1] := Edit2;
SArrayEdit[2] := Edit3;
SArrayEdit[3] := Edit4;
SetLength(DArrayEdit, 4);
end;
//(* 转载敬请注明-本文出处:南山古桃(nsgtao)的百度空间:http://hi.baidu.com/nsgtao/ *)
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
//静态数组
for i := 0 to 3 do
begin
SArrayEdit[i].Text := inttostr(i+1);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i: integer;
begin
//动态数组
DArrayEdit[0] := Edit1;
DArrayEdit[1] := Edit2;
DArrayEdit[2] := Edit3;
DArrayEdit[3] := Edit4;
for i := 0 to 3 do
begin
DArrayEdit[i].Text := inttostr((i+1)*10);
end;
end;
//
//
//
//下面可能比组件数组更简洁。。。
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
begin
//TEdit
for i:=0 to ComponentCount-1 do
begin
if Controls[i] is TEdit then
begin
TEdit(Controls[i]).Text := inttostr(i);
end;
end;
end;