EventHandler inside TFrame?

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

The idea was that each instance of the frame processes its own events using the eventhandler inside the frame. The problem is that the eventhandler not being called.

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

But if I assign an eventhandler outside the scope of 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 just for one button! Since I have many components inside the frame, this will lead to a lot of tasks ... Very ugly code when it should be done directly in the object inspector.

I am using D2007. Any idea of ​​the reason?

Regards Roland

+4
source share
1 answer

The reason it works when you do this from code is because you are replacing a pointer to an event handler, regardless of what happened before.

During development, I have two possible places to install the handler. Suppose I have Frame1 in Unit1, and I put it in MyForm in MyUnit, I will be able to set an event handler in both places.

In your case, you want to set the event handler in the frame itself (Unit1 in my example), since the code it refers to is in the frame itself. If you do this, it should work. If you install an event handler in the place where it is used (MyUnit), then an event handler will be assigned to it.

Delphi is smart enough to still call the event handler from your frame while this event handler was assigned before you added it to the form. If you first added it to the form, and then added a handler to the frame, then there will be no frame. Even worse, if you delete the handler in the form, it will still not name the one that is in the form.

What you need to do is:

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

 inline FrameX: fraVehicleUnitFrame1 

Under it, find

 inherited cmdNewOwner: TButton 

There you should see something like:

 OnClick = FormOldClickHandler 

or maybe

 OnClick = nil 

Delete this OnClick assignment, review the form again and save. Everything should be fine. If now you select a button (or something like cmdNewOwner) on the form, the object inspector should not show anything next to this event.

+11
source

All Articles