Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get name of the Sender object

2,870 views
Skip to first unread message

Calabetta

unread,
Jun 5, 2003, 8:02:41 AM6/5/03
to
Can I get the name of the Sender object, and assign it to a string variable?
E.g.
type Button1: TButton;
...
procedure Button1Click(Sender);
var myString: string;
myString:=...
Result of myString is 'Button1'


Eduardo Andrade Bahiense

unread,
Jun 5, 2003, 8:17:22 AM6/5/03
to
Once Sender is usually TObject and TObject doesn't have a Name property,
you must typecast Sender like

MyString := TControl(Sender).Name
or
MyString := TButton(Sender).Name;

cheers, Eduardo

"Calabetta" <nos...@no-optec-srl.com> escreveu na mensagem
news:3edf324d$1...@newsgroups.borland.com...

Once Sender is usually TObject and TObject doesn't have a Name property,
you must typecast Sender like

MyString := TControl(Sender).Name
or
MyString := TButton(Sender).Name;

cheers, Eduardo

"Calabetta" <nos...@no-optec-srl.com> escreveu na mensagem
news:3edf324d$1...@newsgroups.borland.com...

John Herbster (TeamB)

unread,
Jun 5, 2003, 9:19:14 AM6/5/03
to

"Eduardo Andrade Bahiense" <edu...@fdv.com.br> wrote

> MyString := TControl(Sender).Name
> or
> MyString := TButton(Sender).Name;

Or to be a little safer, I might use some checking first:
If Sender is TControl
then MyString := TControl(Sender).Name
else raise ...
Regards, JohnH

Magnus Oskarsson

unread,
Jun 5, 2003, 9:15:59 AM6/5/03
to

> Once Sender is usually TObject and TObject doesn't have a Name property,
> you must typecast Sender like
>
> MyString := TControl(Sender).Name
> or
> MyString := TButton(Sender).Name;

The Name property is introduced in TComponent, so typecasting to TComponent
will suffice. Note however that the Name property's main purpose is to be
used at design time, not to be messed around with at run-time.

Regards

Magnus Oskarsson

Jens Berke

unread,
Jun 5, 2003, 9:17:47 AM6/5/03
to
Eduardo Andrade Bahiense wrote:
> Once Sender is usually TObject and TObject doesn't have a Name property,
> you must typecast Sender like
>
> MyString := TControl(Sender).Name
> or
> MyString := TButton(Sender).Name;
If Sender isn't a TComponent-descendant this might cause trouble, better
do something like

if Sender is TComponent then
MyString := TComponent(sender).Name
else
MyString := 'not a TComponent';

Jens

--
____________________________________________________________
email (delete all digits, please): in...@11.jensberke.11.de


Woody (TMW)

unread,
Jun 5, 2003, 2:23:23 PM6/5/03
to
"John Herbster (TeamB)" <herb-sci1_at_sbcglobal.net> wrote in message
news:3edf...@newsgroups.borland.com...

Or, to be even more generic, use RTTI. <g>

--

Woody (TMW)

"Start every day off with a smile and get it over with."
-- W. C. Fields


Edwin Walker

unread,
Jun 5, 2003, 2:37:28 PM6/5/03
to
Magnus,

You are right about the use of the Name property at design time, however,
I have used it many times at run time. Not to change the name, but to
determine which controls, of the same type, I am dealing with. I have also
used the tag property for this, but the name works just fine also.

Edwin

0 new messages