Board index » delphi » ??Question....How to get the name or caption of the Sender:TObject;

??Question....How to get the name or caption of the Sender:TObject;

I have multiple menu items sharing the same OnClick Event, how can I get
the name and/or the caption of the menu item from the sender:TObject?

 

Re:??Question....How to get the name or caption of the Sender:TObject;


Quote
Mike Philbrick wrote:

> I have multiple menu items sharing the same OnClick Event, how can I get
> the name and/or the caption of the menu item from the sender:TObject?

You can see who the Sender is in an if .. then statement like this:

if Sender = MenuItem1 then
        //do stuff here
if Sender = MenuItem2 then
        //do other stuff

where MenuItem1 and MenuItem2 are the Names of the menu items.

-Mitch

Re:??Question....How to get the name or caption of the Sender:TObject;


Cast the Sender parameter to the correct type...

Label1.Caption := (Sender as TMenuItem).Caption;

Mitch Planck <anti-spam.mpla...@borders.com> wrote in article
<33F36AA4.6...@borders.com>...

Quote
> Mike Philbrick wrote:

> > I have multiple menu items sharing the same OnClick Event, how can I
get
> > the name and/or the caption of the menu item from the sender:TObject?
> You can see who the Sender is in an if .. then statement like this:

> if Sender = MenuItem1 then
>    //do stuff here
> if Sender = MenuItem2 then
>    //do other stuff

> where MenuItem1 and MenuItem2 are the Names of the menu items.

> -Mitch

Re:??Question....How to get the name or caption of the Sender:TObject;


In article <01bca8dd$7bc86260$cbb8a4cd@mjp>,
   "Mike Philbrick" <mjphilbr...@westernsurety.com> wrote:

Quote
>I have multiple menu items sharing the same OnClick Event, how can I get
>the name and/or the caption of the menu item from the sender:TObject?

In the OnClick, you can go:

  if Sender is TMenuItem then
    with TMenuItem(Sender) do
      Form2.Caption := Caption+' was clicked!';

(Or you could just check if Sender=MenuItem1 if you're just trying to
 decide which menu item was clicked - but be 'generic' if you can)

  --=- Ritchie Annand

Re:??Question....How to get the name or caption of the Sender:TObject;


Quote
Mike Philbrick wrote:

> I have multiple menu items sharing the same OnClick Event, how can I get
> the name and/or the caption of the menu item from the sender:TObject?

Something like:

if Sender is TMenu then
 begin
  if Sender(TMenu).Name = 'NameInQuestion' then
   Sender(TMenu).Caption := 'NewValue';
 end;{if}

I believe that's right ...

Rkr
--

          \|||/
          /'^'\
         ( 0 0 )
-----oOOO--(_)--OOOo----------
Reid Roman
E-Mail : rkro...@pacbell.net
Delphi Enthusiast, Hobbiest ..
U.S.A
------------------------------

Re:??Question....How to get the name or caption of the Sender:TObject;


Quote
Reid Roman <rkro...@pacbell.net> wrote:
>if Sender is TMenu then
> begin
>  if Sender(TMenu).Name = 'NameInQuestion' then
>   Sender(TMenu).Caption := 'NewValue';
> end;{if}

Close...  it's TMenu(Sender).Whatever, not the other way around.

Other Threads