使用 Sender 参数

博客介绍了在事件句柄中,Sender参数可指示接收事件并调用句柄的组件。多个组件可共享事件句柄,通过结合Sender参数与if...then...else语句,能让事件根据调用组件产生不同行为,还给出了button1按钮OnClick事件的代码示例。

In an event handler, the Sender parameter indicates which component received the event and therefore called the handler. Sometimes it is useful to have several components share an event handler that behaves differently depending on which component calls it. You can do this by using the Sender parameter in an if...then...else statement. For example, the following code displays the title of the application in the caption of a dialog box only if the OnClick event was received by Button1.

在一个事件句柄,Sender参数指的是哪一个组件接受该事件并因此调用了该事件。有时当几个组件共用同一事件句柄是很有用的,事件产生不同的行为是由调用它的组件所决定。要做到这点,你只要将Sender参数与if……then……else语句结合使用就可以了。下列代码示范了当button1按钮的OnClick事件发生时将程序的标题显示在指定对话框的标题上。
procedure TMainForm.Button1Click(Sender: TObject);
begin
if Sender = Button1 then 
  AboutBox.Caption := 'About ' + Application.Title 
else
  AboutBox.Caption := ''; 
AboutBox.ShowModal;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值