Delphi 中的Sender:TObject 对象用法

Delphi 中的 Sender:TObject

1、Sender介绍

Sender的类型是Tobject,是Tobject的派生类。

Sender是一个TObject类型的参数,它告诉Delphi哪个控件接收这个事件并调用相应的处理过程。

例如:

procedure TForm1.Button1Click(Sender:TObject);
begin
    //
end;

当点击BUTTON1时,会产生一个Button1Click事件,系统会把Button1传递给Button1Click过程作为参数,也就是 Sender ,此时的Sender 是Button1

2、Sender的用法:

2.1 当控件用 (as),格式:

(Sender as 控件名).控件属性:=...

例如:

procedure TForm1.Edit1Click(Sender: TObject);
begin
  with Sender as TEdit do
  begin
    text:=’Hello 滔Roy’;
  end;
  if Sender is TButton then showmessage((Sender as TButton).Caption)
end;

2.2 对象判断(多个事件中处理同样的事情)

Procedure TForm1.Button1Click(Sender:TObject);
begin
if Sender=Button1 then  
//事件
if Sender=Button2 then
//事件
end;

2.3 使用保留字IS来 使用 Sender

procedure TForm1 xxx(Sender:TObject);
begin
  if(sender is Tedit) then
  showmessage(′this is a editbox′);
  if(sender is TMemo) then
  showmessage(′this is memo′);
end;

  

创建时间:2020.09.17  更新时间:

博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你所有帮助,谢谢!
原文地址:https://www.cnblogs.com/guorongtao/p/13685379.html