一.概念
Sender就是事件的载体,其类型是Tobject,它告诉Delphi哪个控件接收这个事件并调用相应的的处理过程。Sender往往代表了 “事件的触发者”或者说“这个外挂的事件处理函数的调用者”
比如:
procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption:=Tbutton(sender).Caption; //就是把按钮的名字显示出来 end;
二.用法
例一:
procedure TForm1.Edit1Click(Sender: TObject); begin with Sender as TEdit do //将sender作为TEdit类使用 ,as操作符是类型转换。 begin Text:='hello'; end; end;
例二:
procedure TForm3.Image9Click(Sender: TObject); begin DiaoYong(Sender); end; procedure TForm3. DiaoYong(Sender: TObject); begin with Sender as TImage do begin Image0.Picture:=Picture;//等号后的Picure即Sender的Picture end; end;
例三:
Procedure TForm1.Button1Click(Sender:TObject); begin if Sender=Button1 then begin //Do Sth; end; if Sender=Button2 then begin //Do Sth; end; end; procedure TForm1.Button2Click(Sender:TOBJect); begin Button1Click(Button2); end;