Change TCollectionItem Label in Delphi Editor

The component I'm working on uses TCollection to store links to other components. When elements are edited in the designer, their labels look something like this:

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

How to add meaningful labels (possibly the name of the associated component)? eg.

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

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

+4
source share
2 answers

To display a meaningful name, override GetDisplayName:

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

To display the collection editor when a non-visual component double-clicked, you need to redefine the TComponentEditor editing procedure.

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

... and register the editor:

 RegisterComponentEditor(TMyCollectionComponent, TMyPropertyEditor); 
+5
source

The name displayed in the editor is stored in the DisplayName property of the element. Try installing the code to install something similar when creating the link:

 item.DisplayName := linkedItem.Name; 

Be careful not to change the DisplayName if the user has already set it. This is a serious annoyance to the user.

+1
source

All Articles