procedure MyOnClick(Sender:Tobject); //定义过程
procedure TForm1.MyOnClick(Sender:Tobject);
begin
ShowMessage((Sender as TLabel).Caption+' is clicked');
end;
procedure TForm1.Button1Click(Sender:TObject);
var
i,n:Integer;
labela: TLabel;
begin
for i:=1 to 8 do
begin
labela:=TLabel.Create(Application); // 或self
labela.Parent:=Self; //*
labela.Caption:='labela'+IntToStr(i);
labela.Top:=50;
labela.Height:=20;
labela.Width:=50;
labela.Left:=i*labela.Width+i*10;
labela.OnClick:=MyOnClick;
end;
end;