简体   繁体   English

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

[英]How to invoke a component's property editor at design time

I have created a component derived from TCustomPanel. 我创建了一个从TCustomPanel派生的组件。 On that panel I have a published property of a class derived from TOwnedCollection. 在该面板上,我有一个派生自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. 一切运行良好,单击该属性的对象检查器中的省略号将打开默认的集合编辑器,我可以在其中管理列表中的TCollectionItems。

  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. 我开始创建一个派生自TDefaultEditor的类并注册它。

  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. 任何解决方案或不同的方法将不胜感激。

You aren't using the correct editor, so far as I can tell. 就我所知,你没有使用正确的编辑器。 TDefaultEditor is described thus: 因此描述了TDefaultEditor

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. 想想当您双击TButton并将其放入OnClick处理程序时会发生什么。

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 . 自从我编写设计时编辑器以来已经很长时间了(我希望我的内存今天正常工作),但我相信你的编辑器应该来自TComponentEditor In order to show the collection editor you call ShowCollectionEditor from the ColnEdit unit. 为了显示集合编辑器,您ShowCollectionEditorColnEdit单元调用ShowCollectionEditor

You can override the Edit method of TComponentEditor and call ShowCollectionEditor from there. 您可以覆盖Edit的方法TComponentEditor并调用ShowCollectionEditor从那里。 If you want to be more advanced, as an alternative you can declare some verbs with GetVerbCount , GetVerb and ExecuteVerb . 如果你想成为更高级的,作为替代,你可以声明一些动词与GetVerbCountGetVerbExecuteVerb If you do it this way then you extend the context menu and the default Edit implementation will execute verb 0. 如果你这样做,那么你扩展上下文菜单,默认的Edit实现将执行动词0。

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. 按照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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在运行时注册组件和属性编辑器? - How to register a component and property editor at run-time? 如何在Delphi中为组件正确调用自定义组件编辑器表单 - How to properly invoke a custom component editor form for a component in delphi 如何为ADO连接字符串实现设计时属性编辑器? - How do I implement a design time property editor for an ADO connection string? 如何修复Delphi组件与TFont属性,在设计时“无法将NIL分配给TFont”? - How to fix Delphi component with TFont property that gets “cannot assign NIL to a TFont” at design time? 我可以在设计时在组件属性中放置方法调用吗? - Can I put a method call in a component property at design time? 如何为FireMonkey组件添加设计时图标? - How to add design time icon for FireMonkey component? 如何在运行时创建相当于设计时的组件 - How to create a component at runtime, equivalent to design time 如何在设计时轻松找到组件 - How to find component in design time easily 如何在设计时从自定义TComponentEditor显示ActionList编辑器 - How to show ActionList editor from custom TComponentEditor at design time 在设计时如何在Objectinspector中将Imagelist属性与ImageIndex属性相关联? - How to associate Imagelist property with ImageIndex property in Objectinspector at design time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM