tags:

views:

204

answers:

3

What does (Sender:TObject) mean?? As in:

procedure TForm1.Button1Click(Sender:TObject);

var
s: Integer;

begin
.....
.....
end;
+13  A: 

Sender is a reference to the component that fired the event. In this case, Sender is going to be the button the user clicked which called your Button1Click event.

This is useful when you have several components that call the same event and you need to figure out which component caused the event to be fired.

For instance, you could do something like:

if Sender = Button1 then
// ...
Jeff Wilhite
@Jeff: you might want to rephrase this as "Sender is a reference to the component". (at the end you could mention that in Win32 the Sender technically is a pointer, but in other environments it does not need to be).
Jeroen Pluimers
Or more generic: if (Sender is TButton) ...if (Sender is TEdit)...
Neftalí
@Jeroen: Yeah, I'm actually a C++ Builder user. We share the VCL (Visual Component Library) with Delphi. So, Sender is a pointer for me. Thanks for the reminder to translate.
Jeff Wilhite
+7  A: 

First web search result:

Understanding the Sender parameter in Delphi Event Handlers

Johannes Sasongko
There should be an "I don't know how to Google" badge that we can "award" users.
Warren P
No, there shouldn't, @Warren. That would just be mean.
Rob Kennedy
A: 

Hi,

Sender is a parameter which is often used when triggering events (calling Event Handlers). Most of the time I would describe it as the Object from which the Event is triggered.

But you could actually pass any object to a Sender parameter as long as it inherits from TObject in this specific case. As Jim mentioned already, in this specific cast you added an event handler to the OnClick event of Button1. So when the Button1 gets clicked, the method Tform1.ButtonClick will be executed and the sender will contain a reference to the instance o f the button you clicked.

Regards,

Stefaan

Stefaan
There is no object that does not inherit from `TObject`.
Smasher
Think I expressed myself incorrectly. You can pass anything in the Sender parameter as long as it's a TObject.
Stefaan