通知一个控件另一个控件有状态更改
我正在使用 C++Builder Enterprise 并且需要一些想法。
我有一个表单,上面有一堆 TButton
和 TSpeedButton
控件。我想要发生的是,当按下给定按钮时,我想禁用它和其他几个按钮,同时启用其他几个按钮,即状态更改。
问题是我在很多地方复制启用/禁用代码。我考虑过以某种方式使用 TNotifyEvent
委托来提醒按钮状态更改,但我认为委托技术在这种情况下不起作用。我想避免创建一堆 TButton
/TSpeedButton
的子类。
我还想尝试使用 VCL 中提供的技术,因为每个组件都带有一个观察者列表,并且想知道我是否可以以某种方式利用它。
I am using C++Builder Enterprise and need some ideas.
I have a Form with a bunch of TButton
and TSpeedButton
controls on it. What I want to have happen is that, when a given button is pressed, I want to disable it and several others, while enabling several other buttons, i.e. state changes.
The issue is that I am duplicating the enable/disable code in a bunch of places. I've thought about somehow using the TNotifyEvent
delegation to alert the buttons of a state change, but I don't think that delegation technique will work in this situation. I want to avoid creating a bunch of sub-classes of TButton
/TSpeedButton
.
I also would like to try to use techniques that are available from the VCL, as in each component carries an observer list and wonder if I could leverage that somehow.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的 2 美分价值..
我已经为你的问题做了一个概念验证,需要做更多的工作,但我认为这是可行的,并且应该做你想要的。
如果有兴趣,我会做更多的工作并发布。
要查找窗体上某个类类型的所有控件
--- TControlAgent->FindTControl(this, "TButton");
我在表单上添加了4个按钮,向button1单击事件添加了以下代码,
TControlAgent->SetControlProperty("Enabled", "TButton", false, true, "Button3");
在这种情况下,想要类名为 TButton、state = true 或 false 的具有 Enabled 属性的控件,执行该操作,但排除名为 TButton3 的控件,
结果是表单上除按钮 3 之外的所有 TButton 都设置为禁用
(注意:排除选项将是要从任何操作中排除的控件的查找列表,或者能够设置不是该状态的状态,例如 Enabled = !true 或 !false )
在 button3 的单击事件中,我放置了以下代码;
TControlAgent->SetControlProperty("启用", "TButton", true, true, "");
这样做的结果是重新启用所有控件,这只是一个想法,但通过额外的工作,可以对表单集合中任何表单的单个父表单的任何控件执行任何操作。
我还认为有可能触发事件,只是一个疯狂的想法...
使用 berlin 10.1
.cpp 文件的 .h 文件
My 2 cents worth..
I have done a proof of concept for your problem, would need a lot more work but I think it is doable and should do what you want.
I will do more work on it and post if there is interest.
To find all controls of a class type on a form
--- TControlAgent->FindTControl(this, "TButton");
I added 4 buttons on a form, added code bellow to button1 click event,
TControlAgent->SetControlProperty("Enabled", "TButton", false, true, "Button3");
in this case, want controls with Enabled property whose class name is TButton, state = true or false, execute the action but but exclude control named TButton3
There result was all TButtons on the form were set to disable except for button 3
(note: the exclude option would be a lookup list of controls to be excluded from any action or be able to set a state that is not the state eg Enabled = !true or !false )
In the click event of button3 I placed the following code;
TControlAgent->SetControlProperty("Enabled", "TButton", true, true, "");
The result of this is to re-enable all the controls, this is just an idea but with with the extra work it could be possible to execute any action on any control from a single parent form for any form in a collection of forms.
I also think it could be possible to fire events, just a crazy idea...
.h file using berlin 10.1
.cpp file
**** 已更新 *****
TControlAgent 的进度
显然仍在进行中,但此代码有效,创建一个带有 TPanel 的表单,向此添加 6 个 TButton
添加 TControl 代理,按照
TControlAgent 代码 下面的表单代码设置 OnButtonClick 事件
.h
.cpp
表单代码
.cpp
表单有一个 TPanel,上面放置了 6 个 TButton
@运行时 TButton3 被禁用
**** UPDATED *****
Progress of TControlAgent
Obviously still work in progress but this code works, create a form with a TPanel, to this add 6 TButtons
Add a TControl Agent, set the OnButtonClick event as per Form code bellow
TControlAgent code
.h
.cpp
Form Code
.cpp
Form has a TPanel with 6 TButtons placed on it
@run time TButton3 is disabled