Delphi TCollection-可以防止在运行时更改项目吗? [英] Delphi TCollection - Possible to prevent changing items in run-time?

查看:88
本文介绍了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.

推荐答案

您可以覆盖 Assign()和<$ c $ TCollection 的c> Notify()方法可被通知添加和删除,并覆盖 SetIndex() TCollectionItem 的code>方法将被通知重新编制索引。对于添加/删除,抛出异常以拒绝该操作(在添加的情况下,您将必须释放添加的新项目)。要重新编制索引,只需不执行任何操作即可退出。

You can override the Assign() and Notify() methods of TCollection to be notified of adds and deletes, and override the SetIndex() method of TCollectionItem 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.

要区分运行时和设计时,请遍历 TCollection的所有者链(以防它嵌套在其他类中)直到找到 TComponent ,然后您可以检查其 ComponentState csDesigning 标志的c $ c>属性。

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 a TComponent, then you can check its ComponentState property for the csDesigning flag.

这篇关于Delphi TCollection-可以防止在运行时更改项目吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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