控件对象引用

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);
 1 var
 2   i: Integer;
 3 const
 4   NamePrefix = 'MyEdit';
 5 begin
 6   for i := 1 to 20 do begin
 7     TEdit.Create(Self).Name := NamePrefix + IntToStr(i);
 8     with TEdit(FindComponent(NamePrefix + IntToStr(i))) do
 9     begin
10       Left := 10;
11       Top := i * 20;
12       Parent := self;
13     end;
14   end;
15 end;


**********************************************************************

动态创建控件(属性和Tag)并绑定事件test

 1 procedure TForm1.test(Sender: TObject);  
 2 var  
 3    l_busin_flag:Integer;  
 4 begin  
 5    l_busin_flag:=(Sender as TButton).Tag;  
 6    ShowMessage(IntToStr(l_busin_flag)+'Hello');  
 7 end;  
 8   
 9 procedure TForm1.FormCreate(Sender: TObject);  
10 var  
11   btns: TButton;  
12   i:Integer;  
13 begin  
14   for i:=0  to  5 do  
15   begin  
16       btns:=TButton.Create(Self);  
17       btns.Width:=100;  
18       btns.Height:=20;  
19       btns.Caption:=IntToStr(i)+'动态按钮';  
20       btns.OnClick:=test;  
21       btns.Left:=i*100;  
22       btns.Tag:=i;  
23       btns.Parent:=Self;  
24   end;  
25 end;  
原文地址:https://www.cnblogs.com/zhrong/p/5670774.html