panelc++buildertscrollboxscrollbox

Assigning TScrollBox event during runtime - incompatible types


Using RAD Studio 10.4.2:

I create TScrollBox during runtime:

TScrollBox* sb = new TScrollBox(this);
sb->Parent = this;
sb->Align = alClient;
sb->AlignWithMargins = true;
sb->Margins->SetBounds(3,3,3,3);
sb->BorderStyle = bsNone;
sb->VertScrollBar->Smooth = true;
sb->VertScrollBar->Tracking = true;
sb->ParentBackground = true;
sb->OnMouseWheel = ScrollBox1MouseWheel; // Error here

And I want to assign it OnMouseWheel event:

void __fastcall TForm1::ScrollBox1MouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
TPoint &MousePos, bool &Handled)
{
// Some code here
}

The mouse wheel event is just the one I got when I placed it on the form and doubleclicked to generate the above event code.

The error is though:

[bcc32c Error] assigning to 'Vcl::Controls::TMouseWheelEvent' (aka 'void ((__closure *))(System::TObject *, System::Classes::TShiftState, int, const System::Types::TPoint &, bool &) __attribute__((fastcall))') from incompatible type 'void (__closure *)(System::TObject *, System::Classes::TShiftState, int, System::Types::TPoint &, bool &) __attribute__((fastcall))'

How do I assign the event then, do I need to cast it somehow?


Solution

  • Solved it myself immediately after posting so I am sharing the solution:

    I changed the function definition to:

    void __fastcall TForm1::ScrollBox1MouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
    const TPoint &MousePos, bool &Handled) // -> added const here
    {
    // Some code here
    }
    

    For some reason 10.4.2 GUI generates the function without the const in the TPoint, when you double click from the object inspector, but it expects it when it is being assigned during runtime, so when const is added it compiles just fine.