delphievent-handlingdelphi-2007tframe

EventHandler inside a TFrame?


I have a TForm (TVehicleEditForm) with 3 identical TFrames (TVehicleUnitFrame) inside.

The idea was that every instance of the frame handle own events by a eventhandler inside the frame. The problem is that the eventhandler is not called.

I have tried to assign the eventhandler by code inside the frame by overriding the Create method but the handler is not called in that case either.

But if I assign the eventhandler outside the frame from the form it works fine. Like this:

fraVehicleUnitFrame1.cmdNewOwner.OnClick := fraVehicleUnitFrame1.cmdNewOwnerClick;
fraVehicleUnitFrame2.cmdNewOwner.OnClick := fraVehicleUnitFrame2.cmdNewOwnerClick;
fraVehicleUnitFrame3.cmdNewOwner.OnClick := fraVehicleUnitFrame3.cmdNewOwnerClick;

And this is only for one button! As I have many components inside the frame this would result in many assignments... Quite ugly code when this should be done directly in the object inspector.

I am using D2007. Any idea of the cause ?

Regards Roland


Solution

  • The reason it works when you do it from code, is that you replace the pointer to the event handler, regardless of what was there before.

    At design-time, I have two possible places to set the handler. Suppose I have Frame1 in Unit1 and I place it on MyForm in MyUnit, I'll have the opportunity to set the event handler in both places.

    In your case you want to set the event handler in the frame itself (Unit1 in my example), as the code it refers to is on the frame itself. If you do that, it should work. If you set the event handler on the place it is used (MyUnit), then the event handler will be assigned there.

    Delphi is clever enough to still call the event handler from your frame as long as that event handler was assigned before you added the one in the form. If you first added it on the form and then added the handler on the frame, the last one on the frame is not called. To make matters worse, if you remove the handler in the form it still won't call the one on the frame.

    What you need to do is this:

    Right-Click your form and select View as Text. Scroll down to the frame. It should be something like:

    inline FrameX: fraVehicleUnitFrame1
    

    Beneath that, look for

    inherited cmdNewOwner: TButton
    

    There you should see something like:

    OnClick = FormOldClickHandler
    

    or perhaps

    OnClick = nil
    

    Delete this OnClick assignment, view as form again and save. All should be well. If you now select the button (or whatever cmdNewOwner is) on the form, the object inspector should show nothing next to that event.