views:

70

answers:

2

I need to temporary remove TFrame's OnExit and OnEnter events, so I'm trying to do following:

declare FEnterHandler and FExit Handler:

private:
    // ...
    TControl *FParentControl;
    (__fastcall *(__closure)(TObject*))(TObject*) FEnterHandler;
    (__fastcall *(__closure)(TObject*))(TObject*) FExitHandler;
    // ...

and I intended to use them as typed downhere, but compilation failed on declaration.

__fastcall TProgressForm::TProgressForm(TComponent *O, TControl *PC)
    : TForm(O), FMapProgressData()
{
    FParentControl = PC;
    if (FParentControl)
    {
        TFrame *frame = dynamic_cast<TFrame*>(FParentControl);
        if (frame)
        {
            FEnterHandler = frame->OnEnter;
            FExitHandler = frame->OnExit;
            frame->OnEnter = 0;
            frame->OnExit = 0;
        }
        FParentControl->Enabled = false;
    }
}
//-------------------------------------------------------------------------
__fastcall TProgressForm::~TProgressForm()
{
    if (FParentControl)
    {
        FParentControl->Enabled = true;
        TFrame *frame = dynamic_cast<TFrame*>(FParentControl);
        if (frame)
        {
            frame->OnEnter = FEnterHandler;
            frame->OnExit = FExitHandler;
        }
    }
}

What am I doing wrong?

+3  A: 

I don't know much C++Builder, but can't you just write

TNotifyEvent FEnterHandler;
TNotifyEvent FExitHandler;

? Looks much nicer and is less error-prone.

Ulrich Gerhardt
Oh, thanks, I'd try now!
Max
I do not know VCL, and assumed, that if some predefined class would existed, it'd be named TEventHandler or smth :)) And couldn't find any.
Max
wow. It is sooo strange... Correct typedef would be: typedef void __fastcall (__closure *TNotifyEvent)(System::TObjectTObject* Sender); And I have not found any TObjectTObject type in help.
Max
I don't know if this works in BCB, but in Delphi I would click on the `OnExit` in `frame->OnExit` while holding down Ctrl. This brings one to the definition of the property `OnExit`. From there one can search the class hierarchy upwards until one finds an declaration of `OnExit` where it actually mentions an type. This declaration is in class `TWinControl` and reads `property OnExit: TNotifyEvent read FOnExit write FOnExit;`. But a useful help system probably would be better. :-)
Ulrich Gerhardt
@Ulrich Gerhardt Yes, I did it almost same way, I typed this->OnEnter and waited while IDE shows me the type. And it shown. But the type definition from IDE as-is differs from one from help I found later and one I've defined by myself. Typedefs for function pointers have a little strange syntax in C++, specifically VCL ones, containing __fastcall and __closure qualifiers. It's C++, horrible language :)
Max
A: 

I'd add my own answer, because I found, that typedef from help is rather strange:

typedef void _fastcall (__closure *TNotifyEvent)(System::TObjectTObject Sender)

and written another, more clear.

typedef void (__closure __fastcall *TEventHandler)(TObject*);
//typedef TNotifyEvent TEventHandler;
TEventHandler FEnterHandler;
TEventHandler FExitHandler;

Of course, I recommend to use TNotifyEvent, it is really better.

Max