Board index » delphi » (Sender: TObject) ??

(Sender: TObject) ??

I am coing from a DOS programming background in C++ and am trying to
understand why Delphi created this parameter automatically for it's event
handlers. IS it for generic/abstract classes or something? Can anyone
explain this.

Should I have such a parameter in my own classes and if so why?

Thanks for the time.

Tom.

 

Re:(Sender: TObject) ??


Quote
tomcorc...@aol.com (TomCorcora) wrote:
>I am coing from a DOS programming background in C++ and am trying to
>understand why Delphi created this parameter automatically for it's event
>handlers. IS it for generic/abstract classes or something? Can anyone
>explain this.

It's so you can use a single event handler for more than one component
and test if necessary for which component or what type of componenent
produced the event.

Quote
>Should I have such a parameter in my own classes and if so why?

If you are making provision for your components to handle the usual
events then it is a _very_ good idea for the user's sake to use
the standard procedural types such as TMouseEvent. Anything else
is asking for confusion.

If you are defining your own special events a Sender parameter still
seems like a good idea.

--
Best wishes

John Nurick

e-mail: j.nur...@dial.pipex.com
v-mail: <+44|0> 191 281 1306

Re:(Sender: TObject) ??


Quote
In article <4j5a00$...@newsbf02.news.aol.com> tomcorc...@aol.com (TomCorcora) writes:
>From: tomcorc...@aol.com (TomCorcora)
>Subject: (Sender: TObject) ??
>Date: 25 Mar 1996 00:12:32 -0500
>I am coing from a DOS programming background in C++ and am trying to
>understand why Delphi created this parameter automatically for it's event
>handlers. IS it for generic/abstract classes or something? Can anyone
>explain this.
>Should I have such a parameter in my own classes and if so why?
>Thanks for the time.

Well, who definitvely knows, but it is quite useful if you assign several
objects the same handler - it allows you to tell in the handler which
component called the handler - eg if you have 3 buttons, all with their
onclick assigned to one handler, then in the handler you can say

  if (sender is mybutton3) then
  if (sender is anotherbutton) then

In your own procedures, then their is no need to send this parameter.

HTH

Re:(Sender: TObject) ??


In article <4j5a00$...@newsbf02.news.aol.com>,
   tomcorc...@aol.com (TomCorcora) wrote:

Quote
>I am coing from a DOS programming background in C++ and am trying to
>understand why Delphi created this parameter automatically for it's event
>handlers. IS it for generic/abstract classes or something? Can anyone
>explain this.

>Should I have such a parameter in my own classes and if so why?

It basically tells the event handler which component is responsible for
setting off the event in the first place, as event-handlers can be shared
between any number of controls that can handle that event.  For example, drop
a button on a page, go into your Events page, click on the OnClick handler,
type in MyClickHandler, and press <ENTER>.

You've got an event handler to fill in, as per usual, with that Sender
parameter up top.  Hold your horses for now, and put another button on the
form.  Go to its event page, click on OnClick and then click on the *down
arrow* next to it.  You should see MyClickHandler come up.  Select it, then
double-click it to go to the handler.

Enter something like this:

begin
  if Sender=Edit1 then
    Edit1.Text := 'OUCH!'
  else
    Edit2.Text := 'Ooya!';
end;

Run that, and you'll soon see where Sender comes in handy.

  --=- Ritchie A.

Re:(Sender: TObject) ??


Quote
tomcorc...@aol.com (TomCorcora) wrote:
>I am coing from a DOS programming background in C++ and am trying to
>understand why Delphi created this parameter automatically for it's event
>handlers. IS it for generic/abstract classes or something? Can anyone
>explain this.
>Should I have such a parameter in my own classes and if so why?

Howdy.

The way I understand the (Sender: TObject), it seems to be VERY similar to
the 'this' pointer in C++. At any rate, it references the object which
called the procedure.

medwa...@onramp.net
Mark Edwards
Arlington, Texas

Re:(Sender: TObject) ??


Quote
Ritchie Annand wrote:

> In article <4j5a00$...@newsbf02.news.aol.com>,
>    tomcorc...@aol.com (TomCorcora) wrote:
> >I am coing from a DOS programming background in C++ and am trying to
> >understand why Delphi created this parameter automatically for it's event
> >handlers. IS it for generic/abstract classes or something? Can anyone
> >explain this.

> >Should I have such a parameter in my own classes and if so why?

> It basically tells the event handler which component is responsible for
> setting off the event in the first place, as event-handlers can be shared
> between any number of controls that can handle that event.  For example, drop
> a button on a page, go into your Events page, click on the OnClick handler,
> type in MyClickHandler, and press <ENTER>.

> You've got an event handler to fill in, as per usual, with that Sender
> parameter up top.  Hold your horses for now, and put another button on the
> form.  Go to its event page, click on OnClick and then click on the *down
> arrow* next to it.  You should see MyClickHandler come up.  Select it, then
> double-click it to go to the handler.

> Enter something like this:

> begin
>   if Sender=Edit1 then
>     Edit1.Text := 'OUCH!'
>   else
>     Edit2.Text := 'Ooya!';
> end;

> Run that, and you'll soon see where Sender comes in handy.

>   --=- Ritchie A.

What sender should have done was provide the control's index number.  The
current implementation is a cludge.  As it is now you need to know the
control's name and do a series of if then statements to determine what
control was the sender.

--
Alexander Medwedew
Computer Ventures, Inc.
compv...@tribeca.ios.com
http://tribeca.ios.com/~compvent/

Re:(Sender: TObject) ??


In article <4ja3is$...@news.onramp.net>,
   medwa...@onramp.net (Mark Edwards) wrote:

Quote
>tomcorc...@aol.com (TomCorcora) wrote:

>>I am coing from a DOS programming background in C++ and am trying to
>>understand why Delphi created this parameter automatically for it's event
>>handlers. IS it for generic/abstract classes or something? Can anyone
>>explain this.

>>Should I have such a parameter in my own classes and if so why?

>Howdy.

>The way I understand the (Sender: TObject), it seems to be VERY similar to
>the 'this' pointer in C++. At any rate, it references the object which
>called the procedure.

Actually, this is a bit misleading. Delphi Objects have implicit 'this'
pointers in their object methods, just like C++ (in Object Pascal, one gets
at it explicitly with the keyword 'Self'.) But this is not the same issue.

Events in Delphi are not modelled as methods of the object in question, but
as methods on a container object, like a form. Any object which defines
events, defines procedure variable fields in its definition, to hold the
event handler. But the actual definition of the event handles, is as a
method of a container object, such as a Form. So a Button.Click event runs
in the context of a Form, not a button. Therefore, to access the component
which caused the event, you need to pass an explicit parameter. 'Self' in
the handler, would refer to the Form in which the method was defined, not
the component for which it is the client handler.

It seems a bit confusing at first! But actually, this is a very flexible
system, it lets you build generic handlers callable in several contexts:
example: a handler that can respond to a menu selection, speed button
press, context menu selection, or programmatic command.

--
Brad Aisa <ba...@hookup.net>  web: http://www.hookup.net/~baisa/

1 Mises (M1.00) = 0.01 troy ounce gold; 1 Rand (1R) = 0.01 Mises

"The highest responsibility of philosophers is to serve as the
guardians and integrators of human knowledge."   -- Ayn Rand

Re:(Sender: TObject) ??


In article <315BD7AA....@tribeca.ios.com>,
   Alexander Medwedew <compv...@tribeca.ios.com> wrote:

Quote
>What sender should have done was provide the control's index number.  The
>current implementation is a cludge.  As it is now you need to know the
>control's name and do a series of if then statements to determine what
>control was the sender.

Not really, you can get the control's index number.  Just try this:

index := (Sender as TComponent).ComponentIndex;

//--------------------------------------------------------------------
// David S. Becker                     ADP Dealer Services (Plaza R&D)
// d...@plaza.ds.adp.com                                  (503)402-3236
//--------------------------------------------------------------------

Re:(Sender: TObject) ??


Quote
tomcorc...@aol.com (TomCorcora) wrote:
>I am coing from a DOS programming background in C++ and am trying to
>understand why Delphi created this parameter automatically for it's event
>handlers. IS it for generic/abstract classes or something? Can anyone
>explain this.
>Should I have such a parameter in my own classes and if so why?
>Thanks for the time.
>Tom.

It is intended that the event handlers can handle messages from
multiple objects, so that we can create a single handler for a bunch
of objects that need similar handling. Consequently, the handler needs
to know which object is calling it then. So, with the Sender parameter
it can address the calling object correctly.

You should have it, just for fun :-) or keep it compatible with
others's.

Melv

Re:(Sender: TObject) ??


RA>In article <4j5a00$...@newsbf02.news.aol.com>,
RA>   tomcorc...@aol.com (TomCorcora) wrote:
RA>>I am coing from a DOS programming background in C++ and am trying to
RA>>understand why Delphi created this parameter automatically for it's
RA>event >handlers. IS it for generic/abstract classes or something? Can
RA>anyone >explain this.
RA>>
RA>>Should I have such a parameter in my own classes and if so why?

RA>It basically tells the event handler which component is responsible
RA>for  setting off the event in the first place, as event-handlers can
RA>be shared  between any number of controls that can handle that event.
RA>For example, drop  a button on a page, go into your Events page,
RA>click on the OnClick handler,  type in MyClickHandler, and press
RA><ENTER>.

RA>Run that, and you'll soon see where Sender comes in handy.

I found Sender very important when you make use of the drag and drop
facilities in your app. An example of this is:

I have a listbox on a form with various options. I also have various
objects on a separate form. My user can select an option from the
listbox and drop it on the form or select an object on the from and drop
it beck in the listbox. The trick is that you need to tell the listbox
in which cases it should accept a dragged object. This  is where Sender
comes in because it allows you to determine whether you listbox must
accept the dragged object or not.

Regards.

Gerhard
EMAIL: gerhar...@onwe.co.za
Tel: (H) 011-392-4864      (W) 011-454-3470

* CMPQwk 1.42 927 *COFFEE.EXE Missing - Insert Cup and Press Any Key

Re:(Sender: TObject) ??


In article <315BD7AA....@tribeca.ios.com>,
   Alexander Medwedew <compv...@tribeca.ios.com> wrote:

Quote
>What sender should have done was provide the control's index number.  The
>current implementation is a cludge.  As it is now you need to know the
>control's name and do a series of if then statements to determine what
>control was the sender.

It would be useful in some situations to also provide the index number, but
in an object-oriented world, providing a "Sender" object reference is
clearly an elegant and flexible solution.  You can write event handlers to
handle different buttons that are not even on the same _form_, which you
could not do if you only had an index number.

If you are doing a bunch of if-then's, you might want to consider
redesigning the way your form works.  

-Kyle

*****  Kyle Cordes @ Automation Service  *****  kcor...@mo.net  *****

Re:(Sender: TObject) ??


Quote

> > Enter something like this:

> > begin
> >   if Sender=Edit1 then
> >     Edit1.Text := 'OUCH!'
> >   else
> >     Edit2.Text := 'Ooya!';
> > end;

> > Run that, and you'll soon see where Sender comes in handy.

> >   --=- Ritchie A.

> What sender should have done was provide the control's index number.  The
> current implementation is a cludge.  As it is now you need to know the
> control's name and do a series of if then statements to determine what
> control was the sender.

Uh, I think you are missing something here. To send the control's index
number rather than a pointer to ther sender itself would be kludge of VB
proportions.

When you code
         if Sender = Edit1 then ...
you are just scratching the surface of what you can do. Actuallt you are just
comparing 2 pointers for equality.

Sender is of a type derived from TObject. That means it is an object (actually
it is a pointer to an object, as that's the Delphi Way)
If you really want the sender's Tag then try  the expresion

 x := (Sender as TComponent).Tag;

i.e. you can read & write properties & can call methods against the sender to
your heart's content. Try that with an index number.

Other Threads