DelphiFAQ Home Search:
General :: Programming :: Delphi :: VCL
About the Delphi VCL (Visual Component Library)

Articles:

This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Only the 40 most recently viewed articles are shown.
You can see the full list here.

Featured Article

What is 'Sender:TObject'?

Question:

I would like to know what information is contained in 'Sender' and how I get at it.

Answer:

Normally, Sender is the object that called the routine. So if you have an event handler, that is connected to a TButton, it is the button. But if it is connected to more than one button, then it is the button that caused the event.

- If you know it is a button,you can use 'as':

ShowMessage((Sender as TButton).Caption);

- You can check the type of Sender with 'is':

if Sender is TButton then
ShowMessage(TButton(Sender).Caption);

- You can of course also check for a particular component:

if Sender = TButton1 then
...
else if Sender = TListBox1 then
...

Generated 16:01:33 on Jun 19, 2018