DelphiFAQ Home Search:

What is 'Sender:TObject'?

 

comments4 comments. Current rating: 5 stars (3 votes). Leave comments and/ or rate it.

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
...

Content-type: text/html

Comments:

2006-02-24, 13:56:26
anonymous from Sweden  
rating
2006-09-07, 02:24:05
anonymous from Brazil  
rating
2008-06-05, 14:17:50
anonymous from Germany  
rating
2012-11-12, 05:31:05
coach001 from United States  

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: