Board index » cppbuilder » Compile error when trying to assign event handler to dynamically created object

Compile error when trying to assign event handler to dynamically created object

I've seen a number of postings on how to assign event handlers to
dynamically created objects.  After following the instructions, I end
up with the following compile time error.  Can anyone tell me how to
fix this?  Thanks for any replies.

(In the example below, I tried to create the event handler outside of
any class.  I have also tried creating the event handler as a member
of a class, but had even more errors.)

Alison

The error:

[C++ Error] AppointmentMgr.cpp(194): E2034 Cannot convert
'void (_fastcall *)(TObject *)' to
'void (_fastcall * (_closure )(TObject *))(TObject *)'

The header file:

// outside of any class
void __fastcall TMemoOnClickHandler(TObject *Sender);

The c++ file:

// in a method of a class
new_appointment = new TAppointmentInformation(etc);
(new_appointment->TheMemoBox)->OnClick = TMemoOnClickHandler;

// somewhere down below
void __fastcall TMemoOnClickHandler(TObject *Sender)
{
  ShowMessage("You just clicked this memo box.");

Quote
}

 

Re:Compile error when trying to assign event handler to dynamically created object


Ok, got the first bit figured out (how to attach the event handler),
but OnChange event handler doesn't get invoked when I type in the
TMemo box.  Anyone got any ideas why?  My event handler gets executed
if I attach it to OnClick or OnExit, but not to OnChange.

For those who are wondering how I got the event handler attached,
here's a summary:
1.  Pulled it into a class (called TAppointmentInformation) which
contains
    references to TMemo object.
2.  Connected it to OnChange event using:
    TheMemoBox->OnChange = TMemoOnChangeEventHandler;
3.  Created TMemoOnChangeEventHandler with single parameter (TObject
*) and
    then compiled to see whether compiler complained that other
parameters
    were required.  (Some events require extra parameters for the
handler,
    but OnChange doesn't.)

Here's the code:

// header file
class TAppointmentInformation
{
private:
        void __fastcall TMemoOnChangeHandler(TObject *Sender);

// cpp file
TAppointmentInformation::TAppointmentInformation(etc)
{
  etc
  TheMemoBox = new TMemo(this);
  TheMemoBox->OnChange = TMemoOnChangeHandler;
  etc

Quote
}

void __fastcall TAppointmentInformation::TMemoOnChangeHandler(TObject
*Sender)
{
   ShowMessage("Event handler executed"));
Quote
}

Other Threads