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

发布于 2024-12-05 00:09:46 字数 1028 浏览 2 评论 0原文

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

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

I have created a component derived from TCustomPanel. On that panel I have a published property of a class derived from TOwnedCollection. All is working well and clicking the ellipsis in the object inspector for that property opens the default collection editor where I can manage the TCollectionItems in the list.

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

I would also like to be able to double-click on the panel at design-time and have the collection editor open up by default. I've started off by creating a class derived from TDefaultEditor and registering it.

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

  RegisterComponentEditor(TMyCustomPanel, TMyCustomPanelEditor);

This seems to be run at the right time, but I'm stuck on how to launch the property editor for the collection at that time.

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;

Any solution or different approach would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

后eg是否自 2024-12-12 00:09:46

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

为双击提供默认行为的编辑器,该双击将迭代属性,查找最合适的方法属性进行编辑

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

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

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

You aren't using the correct editor, so far as I can tell. TDefaultEditor is described thus:

An editor that provides default behavior for the double-click that will iterate through the properties looking the the most appropriate method property to edit

This is an editor that responds to double clicks on the form by dropping you into the code editor with a newly created event handler. Think of what happens when you double click a TButton and you are dropped in to the OnClick handler.

It's been a long time since I wrote a design time editor (I hope my memory is working today) but I believe your editor should be derived from TComponentEditor. In order to show the collection editor you call ShowCollectionEditor from the ColnEdit unit.

You can override the Edit method of TComponentEditor and call ShowCollectionEditor from there. If you want to be more advanced, as an alternative you can declare some verbs with GetVerbCount, GetVerb and ExecuteVerb. If you do it this way then you extend the context menu and the default Edit implementation will execute verb 0.

舟遥客 2024-12-12 00:09:46

根据 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;

Following David's correct answer, I would like to provide the completed code that shows the CollectionEditor for a specific property of a UI control when it is double-clicked at design-time.

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