Board index » cppbuilder » Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor


2005-05-09 09:35:41 AM
cppbuilder86
Hi all,
I have created a class which contains many TShapes, TLabels and TPanels.
What I want to be able to do is when creating this class, is passing in a
pointer to OnMouseDown and OnMouseUp events. Thereby, the user of the class
can create their own event handlers for those events. The idea is that by
passing in the event handlers, these event handlers are assigned to the
appropriate events for each of the objects in the class. However, I am
having trouble declaring the constructor:
eg.
class SpecialButton : public TObject {
__published:
TShape *TLCorner;
TShape *TRCorner; // ...etc....
__public:
__fastcall SpecialButton(TMouseEvent mouseDown, TMouseEvent mouseUp);
// ??????
};
Your suggestions on this would be greatly appreciated.
Thanks,
Kyzer
 
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

Sorry - my example should have read with 'public:' instead of '__public:'
"Kyle Dorian" < XXXX@XXXXX.COM >wrote in message
Quote
Hi all,


I have created a class which contains many TShapes, TLabels and TPanels.
What I want to be able to do is when creating this class, is passing in a
pointer to OnMouseDown and OnMouseUp events. Thereby, the user of the
class
can create their own event handlers for those events. The idea is that by
passing in the event handlers, these event handlers are assigned to the
appropriate events for each of the objects in the class. However, I am
having trouble declaring the constructor:

eg.
class SpecialButton : public TObject {
__published:
TShape *TLCorner;
TShape *TRCorner; // ...etc....


__public:
__fastcall SpecialButton(TMouseEvent mouseDown, TMouseEvent
mouseUp);
// ??????

};

Your suggestions on this would be greatly appreciated.

Thanks,

Kyzer


 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

"Kyle Dorian" < XXXX@XXXXX.COM >wrote in message
Quote
However, I am having trouble declaring the constructor:
Please elaborate. What EXACTLY are you having a problem with?
Gambit
 

{smallsort}

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

Quote

>However, I am having trouble declaring the constructor:

Please elaborate. What EXACTLY are you having a problem with?

Hi,
I'm hoping that the problem I am having is fairly simple, as I would like to
pass two parameters into the constructor function - those parameters being
function definitions for OnMouseDown and OnMouseUp events. The trouble that
I'm having is declaring the constructor so that I may pass the event
handlers into the parameter. For example:
class SpecialButton : public TObject {
__published:
TShape *TLCorner;
TShape *TRCorner; // ...etc....
__public:
__fastcall SpecialButton(/*This is where I'm having troubles*/);
};
So that when I create an object of this type, I can pass in two parameters
which are function names to OnMouseDown and OnMouseUp event handlers - eg:
SpecialButton newButton = new SpecialButton(frmMain->MouseDown,
frmMain->MouseUp);
...where frmMain->MouseDown and frmMain->MouseUp are event handler functions
for the OnMouseDown and OnMouseUp events.
I hope that makes better sense.
Thanks.
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

"Kyle Dorian" < XXXX@XXXXX.COM >wrote in message
Quote
I'm hoping that the problem I am having is fairly simple, as I would
like to pass two parameters into the constructor function
You still haven't answered my question. You've just re-stated what you
already stated in your original message. So I will ask again:
What EXACTLY are you having a problem with? Are you having compiler errors?
Linker errors? Runtime errors? Other?
Quote
The trouble that I'm having is declaring the constructor so that I
may pass the event handlers into the parameter.
You already know how to do that, because you showed as much in your original
message. There was nothing wrong with the code you showed. So again, what
EXACTLY are you having a problem with? You need to be VERY VERY VERY
specific.
Gambit
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

Sorry, I was hoping that would be clearer. What I need to know is how to
declare the parameters when declaring the constructor of my class. More
specifically:
__fastcall SpecialButton(/*I want to declare two parameters here which will
allow me to pass in the names of OnMouseDown and OnMouseUp event
handlers*/);
I am getting compiler errors as I do not have the parameters declared
correctly....so that when I use the following line....
SpecialButton newButton = new SpecialButton(frmMain->MouseDown,
frmMain->MouseUp);
// frmMain->MouseUp and frmMain->MouseDown are event handlers for OnMouseUp
and OnMouseDown events
...the two parameters that I pass in do not match the parameters that are
defined in the constructor. Therefore I need to know how to declare the two
parameters in the constructor so that the above line does work correctly and
I don't get compiler errors.
Hopefully that clears things up.
Thanks,
Kyzer
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

Following on from the previous post, to make it even clearer (hopefully),
the answer I require is where the question marks are in the following
constructor declaration....
__fastcall SpecialButton(?????????????);
....so that the following line may be used to call the constructor above:
SpecialButton newButton = new SpecialButton(frmMain->MouseDown,
frmMain->MouseUp);
// frmMain->MouseUp and frmMain->MouseDown are event handlers for OnMouseUp
and OnMouseDown events
Thanks.
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

"Kyle Dorian" < XXXX@XXXXX.COM >wrote in message
Quote
What I need to know is how to declare the parameters when
declaring the constructor of my class.
As I've already said, you already know exactly how to do that. Your
original message showed as much.
Quote
I am getting compiler errors
Ok, now we're finally getting somewhere. What EXACTLY is the compiler
complaining about? What are the actual error messages?
Quote
as I do not have the parameters declared correctly....
Yes, you do.
Quote
SpecialButton newButton = new SpecialButton(frmMain->MouseDown,
frmMain->MouseUp);
<snip>
...the two parameters that I pass in do not match the parameters
that are defined in the constructor.
Yes, they do.
However, I will mention that TControl, which TForm derives from, already has
MouseDown() and MouseUp() methods of its own. Any errors you are getting
are most likely caused by your declaring your own overloaded versions of
those methods, and the compiler not knowing which ones to use. You should
be giving your event handlers moure unique names. What was wrong with using
the default names that the IDE automatically generated for you when you
created the form?
Gambit
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

Quote
>SpecialButton newButton = new SpecialButton(frmMain->MouseDown,
frmMain->MouseUp);
>
>...the two parameters that I pass in do not match the parameters
>that are defined in the constructor.

Yes, they do.

However, I will mention that TControl, which TForm derives from, already
has
MouseDown() and MouseUp() methods of its own. Any errors you are getting
are most likely caused by your declaring your own overloaded versions of
those methods, and the compiler not knowing which ones to use. You should
be giving your event handlers moure unique names. What was wrong with
using
the default names that the IDE automatically generated for you when you
created the form?
Ok, I think the point has been missed. The event handlers for frmMain have
been created correctly and work as expected for the OnMouseDown and
OnMouseUp events. However, I wish to create many 'SpecialButton' objects at
runtime which in-turn require the OnMouseDown and OnMouseUp event handlers
of frmMain. The following code shows how I want the code:
// SpecialButton class
class SpecialButton : public TObject {
__published:
TPanel *firstPanel;
TPanel *secondPanel;
// .....
public:
__fastcall SpecialButton(TMouseEvent mouseDownEvent, TMouseEvent
mouseUpEvent);
};
// SpecialButton constructor
__fastcall SpecialButton::SpecialButton(TMouseEvent mouseDownEvent,
TMouseEvent mouseUpEvent) {
firstPanel = new TPanel(NULL);
firstPanel->OnMouseDown = mouseDownEvent;
firstPanel->OnMouseUp = mouseUpEvent;
secondPanel = new TPanel(NULL);
secondPanel->OnMouseDown = mouseDownEvent;
secondPanel->OnMouseUp = mouseUpEvent;
}
// Creating a new SpecialButton (frmMain->MouseUp and frmMain->MouseDown are
valid event handlers)
SpecialButton *newButton = new SpecialButton(frmMain->MouseUp,
frmMain->MouseDown);
If I run this code, I get the following compiler errors on the line where I
try to create a new SpecialButton:
Could not find a match for SpecialButton::SpecialButton(void, void)
I hope that this explains things further. Thanks for your help so far.
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

"Kyle Dorian" < XXXX@XXXXX.COM >wrote in message
Quote
Ok, I think the point has been missed.
And you are not paying attention. Please do so.
Quote
The event handlers for frmMain have been created correctly
Yes, but you are not using them correctly in regards to this issue.
Quote
However, I wish to create many 'SpecialButton' objects at runtime
which in-turn require the OnMouseDown and OnMouseUp event
handlers of frmMain.
You don't need to keep stating that over and over. We got the point the
first time.
Quote
The following code shows how I want the code:
<snip>
If I run this code, I get the following compiler errors on the line
where I try to create a new SpecialButton:
Could not find a match for SpecialButton::SpecialButton(void, void)
I already explained in my previous message exactly why that is happening.
You apparently have not made the change I suggested.
Gambit
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

Quote
>The following code shows how I want the code:
>
>If I run this code, I get the following compiler errors on the line
>where I try to create a new SpecialButton:
>Could not find a match for SpecialButton::SpecialButton(void, void)

I already explained in my previous message exactly why that is happening.
You apparently have not made the change I suggested.
Sorry that I named the event handlers as MouseDown() and MouseUp() in this
example, but I don't have them named as this in my code and am still getting
the error that I mentioned in the previous message. Directly from my code,
these are how my event handlers are declared (which work correctly):
void __fastcall ScreenPressedDown(TObject *Sender, TMouseButton Button,
TShiftState shift, int X, int Y);
void __fastcall ScreenPressedUp(TObject *Sender, TMouseButton Button,
TShiftState shift, int X, int Y);
So then I wish to create an object of the SpecialButton class as follows:
SpecialButton *newButton = new SpecialButton(frmMain->ScreenPressedDown,
frmMain->ScreenPressedUp);
As stated before, the answer that I require is how to declare the parameters
in the constructor declaration so that the above line works correctly.
Currently I have it declared as:
__fastcall SpecialButton(TMouseEvent mouseDownEvent, TMouseEvent
mouseUpEvent);
...but there is a mismatch between the declaration of the parameters in the
constructor and calling of the constructor previously. Therefore the answer
I require is displayed as the question marks below:
__fastcall SpecialButton(??????????????????);
I don't think it can be explained any clearer than that.
Thanks
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

"Kyle Dorian" < XXXX@XXXXX.COM >wrote in message
Quote
Sorry that I named the event handlers as MouseDown() and MouseUp()
in this example, but I don't have them named as this in my code
Then you should have been showing your ACTUAL code all along. It is very
hard to diagnose people's problems when they show what they are ACTUALLY
doing.
Quote
and am still getting the error that I mentioned in the previous message.
Then please show your ACTUAL code.
Quote
As stated before, the answer that I require is how to declare the
parameters in the constructor declaration so that the above line works
correctly.
You already know how to DECLARE the constructor. Please skip past that
part, there is nothing wrong with it. The problem you are actually having
is PASSING IN the parameters. That is not the same issue as DECLARING them.
Quote
...but there is a mismatch between the declaration of the parameters
in the constructor and calling of the constructor previously.
That is because you are not PASSING IN what the compiler expects you to pass
in. That has nothing to do with DECLARING the parameters in the constructor
itself.
SpecialButton *newButton = new
SpecialButton(&(frmMain->ScreenPressedDown), &(frmMain->ScreenPressedUp));
Quote
Therefore the answer I require is displayed as the question marks below:
YOU ALREADY KNOW THE ANSWER TO THAT! How many times does that have to be
spelled out to you? Please stop asking the same question over and over.
Gambit
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

Quote
>As stated before, the answer that I require is how to declare the
>parameters in the constructor declaration so that the above line works
correctly.

You already know how to DECLARE the constructor. Please skip past that
part, there is nothing wrong with it. The problem you are actually having
is PASSING IN the parameters. That is not the same issue as DECLARING
them.

>...but there is a mismatch between the declaration of the parameters
>in the constructor and calling of the constructor previously.

That is because you are not PASSING IN what the compiler expects you to
pass
in. That has nothing to do with DECLARING the parameters in the
constructor
itself.

SpecialButton *newButton = new
SpecialButton(&(frmMain->ScreenPressedDown), &(frmMain->ScreenPressedUp));

>Therefore the answer I require is displayed as the question marks below:

YOU ALREADY KNOW THE ANSWER TO THAT! How many times does that have to be
spelled out to you? Please stop asking the same question over and over.
Thank you for your help. The answer that I required was as simple as
passing in the pointer to the function, rather than the function itself.
The reason I was asking about the declaration in the constructor was that I
believed that this was the part that was wrong, not actually calling the
constructor, as I believed there was also a mismatch between assigning the
TMouseEvent in the constructor to the OnMouseDown or OnMouseUp events
through all of the changes that I had tried previously.
 

Re:Passing in OnMouseDown and OnMouseUp event handlers to an object's constructor

"Kyle Dorian" < XXXX@XXXXX.COM >wrote in message
Quote
Thank you for your help. The answer that I required was as simple
as passing in the pointer to the function, rather than the function
itself.
Normally, specifying just a function name without paranthesis is the same as
taking the pointer to the function. VCL event closures are a little
different, so the '&' address operator usually has to be used explicitally
when passing them as parameters.
Gambit