重新创建关联时保持 UpDown-Associate 连接
我有一个 TUpDown 控件,其 Associate 设置为 TEdit 子类的实例。编辑类在其重写的 DoEnter 方法中调用 RecreateWnd。不幸的是,这会终止 API 级别的好友连接,从而导致奇怪的行为,例如单击向上向下箭头时。
我的问题是,编辑实例不知道它是某个应该重新连接的 updown 的好友,并且 updown 没有收到其好友丢失的通知。有什么想法可以重新连接两者吗?
I have an TUpDown control whose Associate is set to an instance of a TEdit subclass. The edit class calls RecreateWnd in its overriden DoEnter method. Unfortunately this kills the buddy connection at the API level which leads to strange behavior e.g. when clicking on the updown arrows.
My problem is that the edit instance doesn't know that it is the buddy of some updown to which it should reconnect and the updown isn't notified of the loss of its buddy. Any ideas how I could reconnect the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我注意到 TCustomUpDown.SetAssociate 如何检查 updown 和 buddy 是否具有相同的父级,并使用它来避免重复关联。所以我尝试调用我自己的 RecreateWnd 方法:
等等 - 它有效!
I noticed how TCustomUpDown.SetAssociate checks that updown and buddy have the same parent and uses this to avoid duplicate associations. So I tried calling my own RecreateWnd method:
et voila - it works!
你发现了一些相当不幸的事情。您在应用程序级别的两个控件之间建立了关联,因此您应该能够继续在应用程序级别代码中管理该关联,但 VCL 不提供维护该关联所需的框架。理想情况下,应该有一个通用的关联框架,这样关联的控件就可以互相通知它们应该更新自己。
VCL 以
Notification
方法开始,但它仅通知组件被销毁。我认为您提出的解决方案对于任务来说有点过于具体。编辑控件不一定知道它附加到上下控件,即使知道,也不应该要求它们共享父级。另一方面,为这个问题编写一个完整的通用观察者框架就有点矫枉过正了。我提出一个妥协方案。
从编辑控件上的新事件属性开始:
然后像上面那样重写
RecreateWnd
,但不是所有特定于上下控件的代码,只需触发事件:现在,处理该事件在您的应用程序代码中,您确切地知道哪些控件彼此关联,因此您不必搜索任何内容,也不需要任何父子关系:
You've discovered something rather unfortunate. You set up an association between two controls at the application level, so you should be able to continue to manage that association in application-level code, but the VCL doesn't provide the framework necessary for maintaining that. Ideally, there would be a generic association framework, so associated controls could notify each other that they should update themselves.
The VCL has the beginnings of that, with the
Notification
method, but that only notifies of components being destroyed.I think your proposed solution is a little too specific to the task. An edit control shouldn't necessarily know that it's attached to an up-down control, and even if it does, they shouldn't be required to share a parent. On the other hand, writing an entire generic observer framework for this problem would be overkill. I propose a compromise.
Start with a new event property on the edit control:
Then override
RecreateWnd
as you did above, but instead of all the up-down-control-specific code, simply trigger the event:Now, handle that event in your application code, where you know exactly which controls are associated with each other, so you don't have to search for anything, and you don't need to require any parent-child relationships:
尝试在调用 RecreateWnd 之前将 Associate 属性的值存储在局部变量中,然后再将其设置回来。
Try storing the value of the Associate property in a local variable before you call RecreateWnd, then setting it back afterwards.