Delphi TCollection - 可以防止在运行时更改项目吗?

发布于 2024-12-14 08:25:25 字数 168 浏览 2 评论 0原文

使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情魔剑神 2024-12-21 08:25:26

您可以重写 TCollectionAssign()Notify() 方法以获取添加和删除通知,并重写 SetIndex TCollectionItem 的 () 方法,用于收到重新索引通知。对于添加/删除,抛出异常以拒绝操作(在添加的情况下,您必须释放添加的新项目)。对于重新索引,只需退出而不执行任何操作。

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文