动态产生构件并相应事件,动态产生构件并相应事件
【 tulaoshi.com - Delphi 】
方法:
             var
                  MyBu : TBUTTON;
             begin
                     MyBu:=TBUTTON.Create(Form1);
                     MyBu.Parent:=Form1;
                     MyBu.Left:=10;
                     MyBu.Top:=10;
                     MyBu.OnClick:=MyOnClick;
                     MyBu.Show;
             end;
    注:MyOnClick为响应的事件过程.
另:
㈠、运行时生成可视控件:以下以TEdit 控件为例
1.在Form的Public中定义TEdit控件
  Edit1:TEdit;
2.在需要生成的地方加入以下代码:
  Edit1:=TEdit.Create(Self);
  Edit1.Parent:=Form1;
  Edit1.Left ?:=20;
  Edit1.Top :=20;
  Edit1.Text :='Edit1 Text';
3.使用完毕后,释放分配的资源
  if  Assigned(Edit1) then Edit1.Free; 
  ㈡、运行时生成非可视控件:以下以 TTimer控件为例
1.在Form的Public中定义TTimert控件
  Timer1:TTimber;
2.在需要生成的地方加入以下代码:
  Timer1:=TTimer.Create(Self); 
  Timer1.OnTimer:=YourAction;
YourAction是自己定义的OnTimer事件,使用
procedure TForm1.YourAction(Sender:TObject); 完成
3.使用完毕后,释放分配的资源 
  if  Assigned(Timer1) then Timer1.Free;
来源:http://www.tulaoshi.com/n/20160129/1492545.html