delphidelphi-2009tcollectionitem

Change the label of a TCollectionItem in the Delphi editor


A component I am working on uses a TCollection to hold links to other components. When the items are edited in the designer their labels look something like this:

0 - TComponentLink
1 - TComponentLink
2 - TComponentLink
3 - TComponentLink

How do I add meaningful labels (the name of the linked component perhaps)? e.g.

0 - UserList
1 - AnotherComponentName
2 - SomethingElse
3 - Whatever

As a bonus, can you tell me how to make the collection editor appear when the component is double clicked?


Solution

  • To display a meaningful name override GetDisplayName:

    function TMyCollectionItem.GetDisplayName: string; 
    begin 
      Result := 'My collection item name'; 
    end;
    

    To display a collection editor when the non visual component is double clicked you need to override the TComponentEditor Edit procedure.

    TMyPropertyEditor = class(TComponentEditor)
    public
      procedure Edit; override; // <-- Display the editor here
    end;
    

    ... and register the editor:

    RegisterComponentEditor(TMyCollectionComponent, TMyPropertyEditor);