如何在设计时调用组件的属性编辑器

时间:2011-09-16 14:41:43

标签: delphi components property-editor townedcollection

我创建了一个从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;

任何解决方案或不同的方法都将受到赞赏。

2 个答案:

答案 0 :(得分:9)

据我所知,您没有使用正确的编辑器。因此描述了TDefaultEditor

  

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

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

我写了一个设计时编辑器已经很久了(我希望我的记忆今天正常工作),但我相信你的编辑应该来自TComponentEditor。要显示收藏编辑器,请从ShowCollectionEditor单元调用ColnEdit

您可以覆盖Edit的{​​{1}}方法并从那里调用TComponentEditor。如果您想要更高级,可以使用ShowCollectionEditorGetVerbCountGetVerb声明一些动词。如果你这样做,那么你扩展上下文菜单,默认的ExecuteVerb实现将执行动词0。

答案 1 :(得分:5)

根据David的正确答案,我想提供完整的代码,以便在设计时双击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;