0

I'm trying to create and register a custom editor for TCollection in Delphi XE5. I tried several things, including inheriting the TCollectionProperty class. I figured it's enough to override the GetColOptions function as follows:

function TMycollectionEditor.GetColOptions: TColOptions;
begin
  Result: = [];
end;

But nothing happened, it is still possible to add, remove and move items. so by searching more I found the code of the Edit function and wrote the following:

procedure TMycollectionEditor.Edit;
var
  ACollection: TCollection;
  APersistent: TPersistent;
begin
  APersistent: = GetComponent (0);

  while (APersistent <> nil) and not (APersistent is TComponent)
    APersistent: = GetPersistentOwner (APersistent);

  ACollection: = TCollection (TMy component (APersistent) .MyCollection);

  ShowCollectionEditorClass (Designer, GetEditorClass, TComponent (APersistent), ACollection, GetName, GetColOptions);
end;

This also did not change anything in the native editor's workings.

What am I doing wrong? Is this a good practice to do what I'm trying to do? Note: my goal is not the same as this post

Update

I made a temporary change to the method that works (except via context menu or double click [using the ExecuteVerb method of the inherited property of TCollection])

    procedure TMycollectionEditor.Edit;
    var
      ACollection: TCollection;
      APersistent: TPersistent;
    begin
      APersistent: = GetComponent (0);

      while (APersistent <> nil) and not (APersistent is TComponent)
        APersistent: = GetPersistentOwner (APersistent);

      ACollection: = TCollection (TMy component (APersistent) .MyCollection);
//    [update bellow]
      with ShowCollectionEditorClass (Designer, GetEditorClass, TComponent (APersistent), ACollection, GetName, GetColOptions) do
      begin
        AddCmd.Enabled := False;
        ListView1.DragMode := dmManual;
        ToolBar1.Visible := False;
        ToolBar2.Visible := False;
      end;

    end;
AnselmoMS
  • 467
  • 3
  • 18
  • 1
    "*I'm trying to create and register a custom editor for `TCollection`" - why? What are you trying to accomplish that the native editor can't handle? If you just want to block changes to your component's TCollection items at design-time, your actual component should be handling that in code when the `csDesigning` flag is enabled in its `ComponentState` property. Don't try to do this via a custom design editor. – Remy Lebeau Feb 23 '18 at 19:32
  • @RemyLebeau, The goal is to allow the change of properties of collection items in design-time, but these can not be changed in order or quantity of items. So I figured it was the best one to change the publisher's behavior. I think the purpose of the GetColOptions function is this, or not? – AnselmoMS Feb 23 '18 at 20:06
  • "*but these can not be changed in order or quantity of items*" - then why are you using `TCollection` at all? Sounds like you should use a private array/TList with a design-time editor that displays a custom TForm, not try to circumvent how `TCollection` is meant to work. But, in any case, to prevent reordering collection items, you can override the virtual `TCollectionItem.SetIndex()` method to raise an exception at design-time. To prevent adding/removing collection items, you can override the virtual `TCollection.Notify()` method to raise an exception. – Remy Lebeau Feb 23 '18 at 21:27
  • @RemyLebeau, _"Sounds like you should use a private array/TList with a design-time editor"_ - I don't know how do this for a published property (stream to DFM). Also, i did try to override the (`SetIndex`, `Notify`) like you saw above, but, in design-time, aparently the default editor do a cast to TCollection and is possible to Add, remove and reindex items. – AnselmoMS Feb 26 '18 at 11:13
  • But, my big question is: why does the `GetColOptions` parameter have no effect on the editor's operation when executing the `ShowCollectionEditorClass` method? – AnselmoMS Feb 26 '18 at 11:18
  • custom streaming is done by overriding the component's `DefineProperties()` method, calling `Filer.DefineProperty()` to provide reading/writing callbacks. And casting to the base `TCollection` doesn't prevent virtual methods from being called (that is the whole point). I can't answer your `GetColOptions` question because I've never heard or seen `TCollectionProperty` before (I can't find it in my IDE's sources) and I've never worked with `ShowCollectionEditorClass` before – Remy Lebeau Feb 26 '18 at 15:36

0 Answers0