Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top