Board index » cppbuilder » Which component generated the event ?

Which component generated the event ?


2008-07-12 06:17:12 AM
cppbuilder1
Suppose I have an array of sliders (26 of them), generated dynamically with the 'new' function, and dynamically placed
in the right place on a form.
All of them have the same address for the routine that manages the OnMouseEnter event.
How can I inside that routine know which slider generated the event ? I thought of using the 'this' pointer, but it
merely points to the form that owns those sliders.
Is there a way to know that, short of writing 26 OnMouseEnter different routines ?
Thanks
Alberto di Bene
 
 

Re:Which component generated the event ?

"Alberto" < XXXX@XXXXX.COM >wrote in message
Quote
How can I inside that routine know which slider generated the event ?
That is what the Sender parameter is for.
Quote
I thought of using the 'this' pointer
No. The 'this' pointer will point to the form object that the event handler
is a member of.
Quote
but it merely points to the form that owns those sliders.
That is where it is supposed to point.
Gambit
 

Re:Which component generated the event ?

Remy Lebeau (TeamB) wrote:
Quote

>How can I inside that routine know which slider generated the event ?

That is what the Sender parameter is for.

Gambit,
thanks for your answer. Well, initially I had given a thought to the parameter Sender, but it doesn't seem to point
to the object that is causing the event, at least not in the way I had supposed it would.
For example, I have given of course a different value to the Name property of each one of those 26 sliders, as an aid
to identify them. But Sender->Name is not a property that is known to the compiler.
Would you please care to briefly explain me how to use the Sender pointer ?
Many thanks
Alberto di Bene
 

{smallsort}

Re:Which component generated the event ?

Alberto wrote:
Quote
Remy Lebeau (TeamB) wrote:
>
>>How can I inside that routine know which slider generated the
event ?
>
>That is what the Sender parameter is for.
>

Gambit,


thanks for your answer. Well, initially I had given a thought to
the parameter Sender, but it doesn't seem to point to the object that
is causing the event, at least not in the way I had supposed it would.

For example, I have given of course a different value to the Name
property of each one of those 26 sliders, as an aid to identify them.
But Sender->Name is not a property that is known to the compiler.

Would you please care to briefly explain me how to use the Sender
pointer ?

Many thanks

Alberto di Bene
You need to convert the Sender pointer to the appropriate type.
VCL objects types are a hierarchy. The Sender argument is a pointer
to TObject, which does not have a Name property.
TTrackBar (your slider) is derived from TWinControl which is derived
from TControl which is derived from TComponent which is derived from
TPersistent which is derived from TObject.
It is TControl that has the Name Property (TObject, TPersistent, and
TComponent do not).
So, to extract a name from a Sender you need to do this;
TControl *named_thing = dynamic_cast<TControl *>(Sender);
AnsiString name;
if (named_thing != NULL) // we have a Name property
name = named_thing->Name;
else
name = "Unnamed";
If you want to change properties of the slider itself, you would do
this;
TTrackBar *trackbar_thing = dynamic_cast<TTrackBar *>(Sender);
if (trackbar_thing != NULL) // sender is a track bar
trackbar_thing->Position = trackbar_thing + 42;
// so move the slider
 

Re:Which component generated the event ?

You can also use the Tag property of each control, so for instance the tags
could be set
1....n and then you can use these to determine which element of an array is
used by the control.
HTH Pete
"Bob" < XXXX@XXXXX.COM >wrote in message
Quote
Alberto wrote:

>Remy Lebeau (TeamB) wrote:
>>
>>>How can I inside that routine know which slider generated the
>event ?
 

Re:Which component generated the event ?

"Alberto" < XXXX@XXXXX.COM >wrote in message
Quote
Well, initially I had given a thought to the parameter Sender, but it
doesn't seem to point to the object that is causing the event
Yes, it does.
Quote
For example, I have given of course a different value to the Name
property of each one of those 26 sliders, as an aid to identify them.
But Sender->Name is not a property that is known to the compiler.
The Sender parameter is a generic TObject pointer. You have to type cast it
first in order to access any type-specific properties and methods, ie:
void __fastcall TForm1::SliderMouseEnter(TObject *Sender)
{
TSlider *slider = static_cast<TSlider*>(Sender);
// use slider as needed...
}
Gambit
 

Re:Which component generated the event ?

Remy Lebeau (TeamB) wrote:
Quote

"Alberto" < XXXX@XXXXX.COM >wrote in message
news:4879de19$ XXXX@XXXXX.COM ...

>Well, initially I had given a thought to the parameter Sender,
>but it doesn't seem to point to the object that is causing the event

Yes, it does.

>For example, I have given of course a different value to the Name
>property of each one of those 26 sliders, as an aid to identify
>them. But Sender->Name is not a property that is known to the
>compiler.

The Sender parameter is a generic TObject pointer. You have to type
cast it first in order to access any type-specific properties and
methods, ie:

void __fastcall TForm1::SliderMouseEnter(TObject *Sender)
{
TSlider *slider = static_cast<TSlider*>(Sender);
// use slider as needed...
}

Bear in mind that static_cast will force the conversion, whether
Sender is a TSlider or not. If the actual type of Sender is
not certain (i.e you can't make an assumption about what it is)
then a dynamic_cast conversion is needed.
 

Re:Which component generated the event ?

Bob wrote:
Quote
>The Sender parameter is a generic TObject pointer. You have to type
>cast it first in order to access any type-specific properties and
>methods, ie:
>
>void __fastcall TForm1::SliderMouseEnter(TObject *Sender)
>{
>TSlider *slider = static_cast<TSlider*>(Sender);
>// use slider as needed...
>}
>

Bear in mind that static_cast will force the conversion, whether
Sender is a TSlider or not. If the actual type of Sender is
not certain (i.e you can't make an assumption about what it is)
then a dynamic_cast conversion is needed.
Bob, Pete and Remy,
many thanks indeed. Now things are much clearer, and I can now get rid of those lines of code that checked the mouse
position against the position of each slider, just to know which one was the originator of the event... :-)
Alberto di Bene