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

北战南征 提交于 2019-12-01 04:17:53

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.

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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!