delphi sender_了解Delphi事件处理程序中的Sender参数

delphi sender

事件处理程序和发件人 ( Event handlers and the Sender )

procedure TForm1.Button1Click(Sender: TObject) ;
begin
  ...
end; 
Button1Click 按钮1 OnClick event OnClick事件

The parameter "Sender" references the control that was used to call the method. If you click on the Button1 control, causing the Button1Click method to be called, a reference or pointer to the Button1 object is passed to Button1Click in the parameter called Sender.

参数“ Sender”引用用于调用该方法的控件。 如果单击Button1控件,导致调用Button1Click方法,则将对Button1对象的引用或指针传递给名为Sender的参数中的Button1Click。

让我们分享一些代码 ( Let's Share Some Code )

For example, suppose we want to have a button and a menu item do the same thing. It would be silly to have to write the same event handler twice.

例如,假设我们希望按钮和菜单项执行相同的操作。 必须编写两次相同的事件处理程序是很愚蠢的。

To share an event handler in Delphi, do the following:

要在Delphi中共享事件处理程序,请执行以下操作:

  1. Write the event handler for the first object (e.g. button on the SpeedBar)

    为第一个对象编写事件处理程序(例如,SpeedBar上的按钮)
  2. Select the new object or objects - yes, more than two can share (e.g. MenuItem1)

    选择一个或多个新对象 -是的,可以共享两个以上的对象(例如MenuItem1)

  3. Go to the Event page on the Object Inspector.

    转到 “对象”检查器上的“事件”页面。

  4. Click the down arrow next to the event to open a list of previously written event handlers. (Delphi will give you a list of all the compatible event handlers that exist on the form)

    单击事件旁边的向下箭头以打开以前编写的事件处理程序的列表。 (Delphi将为您提供表单上存在的所有兼容事件处理程序的列表)
  5. Select the event from the drop-down list. (e.g. Button1Click)

    从下拉列表中选择事件。 (例如Button1Click)
procedure TForm1.Button1Click(Sender: TObject) ;
begin
  {code for both a button and a menu item}
  ...
  {some specific code:}
  if Sender = Button1 then
   ShowMessage('Button1 clicked!')
  else if Sender = MenuItem1 then
   ShowMessage('MenuItem1 clicked!')
  else
   ShowMessage('??? clicked!') ;
end; 

Note: the second else in the if-then-else statement handles the situation when neither the Button1 nor the MenuItem1 have caused the event. But, who else might call the handler, you could ask. Try this (you'll need a second button: Button2) :

注意: if-then-else语句中的第二个else用来处理Button1和MenuItem1都没有引起事件的情况。 但是,您可能会问,还有谁可能会打电话给处理程序。 试试看(您需要第二个按钮:Button2):

procedure TForm1.Button2Click(Sender: TObject) ;
begin
   Button1Click(Button2) ;
   {this will result in: '??? clicked!'}
end; 

IS和AS ( IS and AS )

if Sender is TButton then
   DoSomething
else
   DoSomethingElse; 
procedure TForm1.Edit1Exit(Sender: TObject) ;
begin
  Button1Click(Edit1) ;
end; 
{... else}
begin
  if Sender is TButton then
    ShowMessage('Some other button triggered this event!')
  else if Sender is TEdit then
    with Sender as TEdit do
     begin
      Text := 'Edit1Exit has happened';
      Width := Width * 2;
      Height := Height * 2;
     end {begin with}
end; 

结论 ( Conclusion )

As we can see, the Sender parameter can be very useful when used properly. Suppose we have a bunch of Edit boxes and Labels that share the same event handler. If we want to find out who triggered the event and act, we'll have to deal with Object variables. But, let's leave this for some other occasion.

如我们所见,正确使用Sender参数会非常有用。 假设我们有一堆共享相同事件处理程序的“编辑”框和“标签”。 如果我们想找出是谁触发了事件并采取行动,则必须处理Object变量。 但是,让我们将其留在其他场合。

翻译自: https://www.thoughtco.com/understanding-sender-parameter-in-delphi-event-handlers-1058223

delphi sender

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值