views:

164

answers:

2

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?

+1  A: 

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
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
+1  A: 

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