EventHandler里面有一个TFrame? [英] EventHandler inside a TFrame?

查看:145
本文介绍了EventHandler里面有一个TFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TForm (TVehicleEditForm),里面有3个相同的 TFrames (TVehicleUnitFrame)。



这个想法是,框架的每个实例都通过框架内的事件处理程序来处理自己的事件。问题在于$ 事件处理程序不被调用。



我试图分配 eventhandler 通过覆盖Create方法的代码,但是在这种情况下也不会调用处理程序。



但是,如果我分配了 eventhandler 在框架外面,它的工作正常。
像这样:

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

这只是一个按钮!
由于我在框架内部有许多组件,所以会导致许多赋值...
当这个应该直接在对象检查器中完成时,相当丑的代码。


$ b $我正在使用D2007。任何想法的原因?



关于Roland

解决方案

当您从代码中执行此操作时,它会起作用,而不管以前是什么,替换为事件处理程序的指针。



在设计时,我有两个可能的地方设置处理程序。假设我在Unit1中有Frame1,并将其放在MyUnit中的MyForm中,我将有机会在两个地方设置事件处理程序。



在你的情况下,你想要在框架本身(我的例子中为Unit1)中设置事件处理程序,因为它引用的代码是在框架本身上。如果你这样做,它应该工作。如果将事件处理程序设置在使用的位置(MyUnit),那么事件处理程序将被分配到那里。



Delphi足够聪明,仍然可以调用事件处理程序只要在之前分配了事件处理程序,您就可以从窗体中添加一个。如果您首先将其添加到表单上,然后将该处理程序添加到框架上,框架上的那个框不会。
更糟糕的是,如果您删除表单中的处理程序,它仍然不会调用该表单上的处理程序。



您需要做的是这样:



右键单击您的表单,然后选择查看为文本。向下滚动到框架。它应该是这样的:

  inline FrameX:fraVehicleUnitFrame1 

在此之下,寻找

 继承的cmdNewOwner:TButton 

您应该看到以下内容:

  OnClick = FormOldClickHandler 

或者可能

  OnClick = nil 

删除此OnClick赋值,再次查看并保存。一切都应该好如果您现在在表单上选择按钮(或任何cmdNewOwner是),对象检查器应该在该事件旁边显示任何内容。


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

解决方案

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 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 form.

What you need to do is this:

Right-Clcik 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.

这篇关于EventHandler里面有一个TFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆