【问题标题】:How to invoke a component's property editor at design time如何在设计时调用组件的属性编辑器
【发布时间】:2011-11-18 18:28:05
【问题描述】:

我创建了一个从 TCustomPanel 派生的组件。在那个面板上,我有一个从 TOwnedCollection 派生的类的已发布属性。一切正常,单击对象检查器中该属性的省略号会打开默认集合编辑器,我可以在其中管理列表中的 TCollectionItems。

  TMyCustomPanel = class(TCustomPanel)
  private
  ...
  published
    property MyOwnedCollection: TMyOwnedCollection read GetMyOwnedCollection write SetMyOwnedCollection;
  end;

我还希望能够在设计时双击面板并默认打开集合编辑器。我首先创建了一个派生自 TDefaultEditor 的类并注册它。

  TMyCustomPanelEditor = class(TDefaultEditor)
  protected
    procedure EditProperty(const PropertyEditor: IProperty; var Continue: Boolean); override;
  end;

  RegisterComponentEditor(TMyCustomPanel, TMyCustomPanelEditor);

这似乎是在正确的时间运行,但我一直纠结于如何启动该集合的属性编辑器。

procedure TMyCustomPanelEditor.EditProperty(const PropertyEditor: IProperty; var Continue: Boolean);
begin
  inherited;

  // Comes in here on double-click of the panel
  // How to launch collection editor here for property MyOwnedCollection?

  Continue := false;
end;

我们将不胜感激任何解决方案或不同的方法。

【问题讨论】:

    标签: delphi components property-editor townedcollection


    【解决方案1】:

    据我所知,您没有使用正确的编辑器。 TDefaultEditor 是这样描述的:

    一个为双击提供默认行为的编辑器,它将遍历属性,寻找最适合编辑的方法属性

    这是一个编辑器,它通过将您放入带有新创建的事件处理程序的代码编辑器来响应表单上的双击。想想当您双击 TButton 并进入 OnClick 处理程序时会发生什么。

    我已经很久没有写设计时编辑器了(我希望我的记忆今天还有效),但我相信你的编辑器应该来自TComponentEditor。为了显示集合编辑器,您从 ColnEdit 单元调用 ShowCollectionEditor

    您可以覆盖TComponentEditorEdit 方法并从那里调用ShowCollectionEditor。如果您想更高级,作为替代方案,您可以使用GetVerbCountGetVerbExecuteVerb 声明一些动词。如果你这样做,那么你扩展上下文菜单,默认的 Edit 实现将执行动词 0。

    【讨论】:

    • 从 TComponentEditor 派生并实现 Get/ExecuteVerb 来调用 ShowCollectionEditor 工作得很好。非常感谢。
    • 哇,我必须承认我有点惊讶它真的那么容易,当我最后一次做这样的事情时,它确实是很久以前的事了!
    【解决方案2】:

    按照大卫的正确答案,我想提供完整的代码,在设计时双击 UI 控件的特定属性时显示 CollectionEditor。

    type
      TMyCustomPanel = class(TCustomPanel)
      private
      ...
      published
        property MyOwnedCollection: TMyOwnedCollection read GetMyOwnedCollection write SetMyOwnedCollection;
      end;
    
    
      TMyCustomPanelEditor = class(TComponentEditor)
      public
        function GetVerbCount: Integer; override;
        function GetVerb(Index: Integer): string; override;
        procedure ExecuteVerb(Index: Integer); override;
      end;
    
    
    procedure Register;
    begin
      RegisterComponentEditor(TMyCustomPanel, TMyCustomPanelEditor);
    end;
    
    function TMyCustomPanelEditor.GetVerbCount: Integer;
    begin
      Result := 1;
    end;
    
    function TMyCustomPanelEditor.GetVerb(Index: Integer): string;
    begin
      Result := '';
      case Index of
        0: Result := 'Edit MyOwnedCollection';
      end;
    end;
    
    procedure TMyCustomPanelEditor.ExecuteVerb(Index: Integer);
    begin
      inherited;
      case Index of
        0: begin
              // Procedure in the unit ColnEdit.pas
              ShowCollectionEditor(Designer, Component, TMyCustomPanel(Component).MyOwnedCollection, 'MyOwnedCollection');
           end;
      end;
    end;
    

    【讨论】:

    • 好例子!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2011-08-30
    • 2011-04-03
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2010-11-03
    • 2014-08-25
    • 2017-09-16
    相关资源
    最近更新 更多