首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在设计时调用组件的属性编辑器

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

Stack Overflow用户
提问于 2011-09-16 22:41:43
回答 2查看 2.7K关注 0票数 7

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

代码语言:javascript
复制
  TMyCustomPanel = class(TCustomPanel)
  private
  ...
  published
    property MyOwnedCollection: TMyOwnedCollection read GetMyOwnedCollection write SetMyOwnedCollection;
  end;

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

代码语言:javascript
复制
  TMyCustomPanelEditor = class(TDefaultEditor)
  protected
    procedure EditProperty(const PropertyEditor: IProperty; var Continue: Boolean); override;
  end;

  RegisterComponentEditor(TMyCustomPanel, TMyCustomPanelEditor);

这似乎是在正确的时间运行,但我被困在如何在那个时候启动集合的属性编辑器。

代码语言:javascript
复制
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;

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-16 23:12:44

据我所知,您使用的编辑器不正确。TDefaultEditor的描述如下:

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

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

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

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

票数 10
EN

Stack Overflow用户

发布于 2011-09-18 22:06:19

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

代码语言:javascript
复制
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;
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7446368

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档