TFrame 内的事件处理程序?
我有一个 TForm
(TVehicleEditForm),里面有 3 个相同的 TFrames
(TVehicleUnitFrame)。
这个想法是框架的每个实例都通过框架内的事件处理程序处理自己的事件。 问题是没有调用eventhandler
。
我尝试通过重写 Create 方法,通过框架内的代码分配 eventhandler
,但在这种情况下也不会调用处理程序。
但是,如果我从表单中将 eventhandler
分配到框架之外,它就可以正常工作。 就像这样:
fraVehicleUnitFrame1.cmdNewOwner.OnClick := fraVehicleUnitFrame1.cmdNewOwnerClick;
fraVehicleUnitFrame2.cmdNewOwner.OnClick := fraVehicleUnitFrame2.cmdNewOwnerClick;
fraVehicleUnitFrame3.cmdNewOwner.OnClick := fraVehicleUnitFrame3.cmdNewOwnerClick;
这只是一个按钮! 由于我在框架内有很多组件,这会导致很多分配...... 当这应该直接在对象检查器中完成时,代码相当丑陋。
我用的是D2007。 知道原因吗?
问候罗兰
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您从代码中执行此操作时,它起作用的原因是您替换了指向事件处理程序的指针,无论之前有什么。
在设计时,我有两个可能的位置来设置处理程序。 假设我在 Unit1 中有 Frame1 并将其放置在 MyUnit 中的 MyForm 上,我将有机会在这两个位置设置事件处理程序。
在您的情况下,您希望在框架本身(我的示例中为 Unit1)中设置事件处理程序,因为它引用的代码位于框架本身上。 如果你这样做,它应该有效。 如果您在使用事件处理程序的地方(MyUnit)设置事件处理程序,则事件处理程序将被分配在那里。
Delphi 足够聪明,只要在您在表单中添加事件处理程序之前分配了该事件处理程序,它仍然可以从您的框架调用该事件处理程序。 如果您首先将其添加到表单上,然后将处理程序添加到框架上,则框架上的最后一个处理程序将不会被调用。
更糟糕的是,如果您删除表单中的处理程序,它仍然不会调用框架上的处理程序。
您需要做的是:
右键单击表单并选择“以文本形式查看”。 向下滚动到框架。 它应该类似于:
在其下方,查找 在
那里您应该看到类似的内容:
或者也许
删除此 OnClick 分配,再次以表单形式查看并保存。 一切都应该很好。 如果您现在选择表单上的按钮(或任何 cmdNewOwner ),则对象检查器不应在该事件旁边显示任何内容。
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:
Beneath that, look for
There you should see something like:
or perhaps
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.
这是老问题了,但我在 Rio 10.3 中也遇到了类似的问题。
在主窗体上,您可以单击“恢复为继承”选项。
此选项位于“对象检查器”中的“事件”选项卡上。 在“OnItemClick”上单击鼠标右键,然后单击选项“恢复为继承”。
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'.