Search code examples
c++c++builderfiremonkeyc++builder-10.1-berlin

Firemonkey custom control click events firing in designer


I have a custom Firemonkey control that has several sub components. These sub components have OnClick events associated with them that are setup in the control's constructor. I have noticed that when I click on the custom control in my design view, the OnClick events of these sub components are getting fired.

Is there a particular setting or best practice I need to employ to prevent this from happening?

Is there something I can check in my C++ code to see if this event is being run in the designer vs at run time? Something like:

void __fastcall MyControlOnClick( TObject * Sender )
{
    if( InDesigner == false )
    {
         //do stuff here
    }
}

Solution

  • Use the ComponentState property. It has a csDesigning flag enabled when your control is being used in the Form Designer.

    void __fastcall MyControl::SubControlClick(TObject *Sender)
    {
        if( !ComponentState.Contains(csDesigning) )
        {
             //do stuff here
        }
    }
    

    Alternatively, simply don't assign the OnClick handlers at design-time to begin with:

    __fastcall MyControl::MyControl(TComponent *Owner)
        : TBaseControl(Owner)
    {
        ...
        FSubControl = new TWhatever(this);
        if( !ComponentState.Contains(csDesigning) )
            FSubControl->OnClick = &SubControlClick;
        ...
    }