4

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?

menjaraz
  • 7,551
  • 4
  • 41
  • 81
norgepaul
  • 6,013
  • 4
  • 43
  • 76

2 Answers2

5

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);
norgepaul
  • 6,013
  • 4
  • 43
  • 76
1

The name displayed in the editor is stored in the item's DisplayName property. Try setting your code to set something like this when you create the link:

item.DisplayName := linkedItem.Name;

Be careful not to change the DisplayName if the user's already set it, though. That's a major UI annoyance.

Mason Wheeler
  • 82,511
  • 50
  • 270
  • 477
  • 3
    Thanks Mason, but unfortunately, it didn't work for me. It dod however lead me to an answer that did work. Simply override the TCollectionItem "GetDisplayName" function e.g. function TMyCollectionItem.GetDisplayName: string; begin Result := 'My collection item name'; end; – norgepaul Sep 12 '09 at 14:33