EventHandler inside a TFrame?

陌路散爱 提交于 2019-12-12 08:14:53

问题


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


回答1:


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.




回答2:


This is old issue but I had the similar problem in Rio 10.3. On the main form you can click "Revert to inherited" option.

This option is on 'Events' tab in 'Object Inscpector'. Click right mouse on 'OnItemClick' and click option 'Revert to inherited'.



来源:https://stackoverflow.com/questions/1109574/eventhandler-inside-a-tframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!