Delphi TCollection - 可以防止在运行时更改项目吗?
使用delphi 7,我有一个 TCollection/TCollectionItem 后代集。它们只能在设计时设置,切勿在运行时修改。我该怎么做?设计时应始终允许所需的任何编辑,但在运行时,我不希望能够添加、删除或重新索引集合中的任何项目。这些项目的属性,是的,我确实希望启用它们。但改变周围的实际项目只能在设计时进行。
Using delphi 7, I have a TCollection/TCollectionItem set of descendents. They are intended to be set up in Design-time only, and should never be modified in Run-time. How can I do this? Design-time should always allow whatever edits are needed, but in Run-time, I don't want to be able to Add, Remove, or Re-index any of the items in the collection. The properties of each of those items, yes, I do want them to be enabled. But changing the actual items around shall only be in design-time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以重写
TCollection
的Assign()
和Notify()
方法以获取添加和删除通知,并重写SetIndex
方法,用于收到重新索引通知。对于添加/删除,抛出异常以拒绝操作(在添加的情况下,您必须释放添加的新项目)。对于重新索引,只需退出而不执行任何操作。TCollectionItem
的 ()要区分运行时和设计时,请遍历
TCollection
的 Owner 链(如果它嵌套在其他类中),直到找到TComponent
,然后您可以检查其ComponentState
属性中的csDesigning
标志。You can override the
Assign()
andNotify()
methods ofTCollection
to be notified of adds and deletes, and override theSetIndex()
method ofTCollectionItem
to be notified of reindexings. For adds/deletes, throw an exception to reject the operation (in the case of add, you will have to free the new item that was added). For reindexing, just exit without doing anything.To differentiate between run-time and design-time, walk through the Owner chain of
TCollection
(in case it is nested inside of other classes) until you find aTComponent
, then you can check itsComponentState
property for thecsDesigning
flag.