delphi - 可以拦截对现有已发布属性的读取

标签 delphi components

我有一个具有已发布属性的组件。想象一下:

  TSomething = (sOne, sTwo, sThree, ...);
  TSomethings = set of TSomething;

  TSomeComponent = TWinControl
  private
    fSomethings: TSomethings;
    function GetSomethings: string;
    procedure SetSomethings(const Value: TSomethings);
  published
    property Somethings: TSomethings read GetSomethings write SetSomethings;
  end;

问题是 TSomething 现在已达到已发布集的最大容量(编译器错误 E2187),因此我需要按如下方式拆分该集:
  TSomething = (sTwo, sThree, ...);
  TSomethings = set of TSomething;

  // Some items moved from TSomething and named with an "o" prefix
  TOtherThing = (oOne, oThirtyThree, oThirtyFour, ...);
  TOtherThings = set of TOtherThing;

  TSomeComponent = TWinControl
  private
    fSomethings: TSomethings;
    fOtherThings: TOtherThings;
    function GetSomethings: string;
    procedure SetSomethings(const Value: TSomethings);
    function GetOtherThings: string;
    procedure SetOtherThings(const Value: TOtherThings);
  published
    property Somethings: TSomethings read GetSomethings write SetSomethings;
    property OtherThings: TOtherThings read GetOtherThings write SetOtherThings;
  end;

TSomethings 中的一些现有项目已移至 TOtherThings(按逻辑对它们进行分类),因此一些用户会打开他们的表单以显示错误:

Error reading SomeComponent1.Somethings: Invalid property value



所以我想在打开表单时默默地处理更改。换句话说,拦截 DFM 中的 Somethings 属性的读取并填充 Somethings 和 OtherThings 属性。

但我似乎找不到办法做到这一点。如果我愿意完全重命名旧属性,覆盖 DefineProperties 应该可以工作,但这会不必要地破坏很多代码(治愈比疾病更糟糕)。

那么还有另一种我忽略的方法吗?

类似的问题:
  • "Size of published set '%s' is >4 bytes". How to fix this compiler error?
  • How to use GetSetProp and SetSetProp from TypInfo unit
  • 最佳答案

    您不需要重命名现有属性,只需将它们设为 public而不是 published在不破坏任何用户代码的情况下为他们禁用 native DFM 流,然后覆盖 DefineProperties()根据需要处理属性的 DFM 流。

    如果您需要维护设计时支持,您可以尝试 creating "fake" properties用于显示对象检查器,以便用户可以根据需要编辑公共(public)属性的值。或者,只编写一个自定义组件编辑器而不是显示一个模态表单来使用 TCheckListBox 编辑值可能会更容易。 , 或 TListView带有复选框。

    关于delphi - 可以拦截对现有已发布属性的读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56123648/

    相关文章:

    android - 如何将屏幕坐标转换为控件坐标

    arrays - Delphi如何释放动态实例化按钮数组?

    angular - 在div中动态加载组件 - Angular5

    dom - ExtJS:组件 VS 元素 VS 其他

    delphi - 如何在单击按钮时动态地将组件添加到 TScrollBox 中?

    delphi - 这是 lambda 吗?如果不是的话是什么?

    html - 使用 Delphi 中的 TWebbrowser 组件向网站发送数据和从网站接收数据

    php - 自定义 SEO 友好链接

    javascript - 如何使用从 api 获取的 React 组件?

    angular - 如何在 Angular 中刷新组件