How to register a TCollectionItem property editor as a list of objects in the object inspector?

221 Views Asked by At

I have a collection published in a component and I would like to be able to choose a collection item in the object inspector without using the method of saving the item's index. I have already published the property of type to item (TCollectionItem), but in the object inspector it appears as a subcomponent with no option to choose another one. I registered an editor so that it could be possible to display the list of items, but it gives an error when clicking on the item dropbox. Below is an illustrative excerpt of the problem:

TMyCollection = class(TCollection)
end;

TMyCollectionItem = class(TCollectionItem)
end;

TMycomponent = class(TComponent)
published
property MyColection: TMyCollection;
property MyChosedItem: TCollectionItem; //< this need to be a list of TCollectionItem
end;

Bellow the Property Editor

type
TMyItemProperty = class(TClassProperty)
public
  function GetAttributes: TPropertyAttributes; override;
  procedure GetValueList(List: TStrings); virtual;
  procedure GetValues(Proc: TGetStrProc); override;
  procedure SetValue(const Value: string); override;
end;


function TMyItemProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paValueList, paSortList, paMultiSelect];
end;

procedure TMyItemProperty.GetValueList(List: TStrings);
var
  Item: TMyCollectionItem;
  Items: TMyCollection;
  I: Integer;
begin
  Items := (GetComponent(0) as TMyComponent).MyColection;

  if Items <> nil then
    for I := 0 to Items.Count-1 do
      List.Add(Items.Items[I].GetNamePath); // Is this the problem ?
end;

procedure TMyItemProperty.GetValues(Proc: TGetStrProc);
var
  I: Integer;
  Values: TStringList;
begin
  Values := TStringList.Create;
  try
    GetValueList(Values);
    for I := 0 to Values.Count - 1 do
      Proc(Values[I]);
  finally
    Values.Free;
  end;
end;

procedure TMyItemProperty.SetValue(const Value: string);
begin
  inherited SetValue(Value);
end;

when i open the property dropbox appear a error:"Invalid TypeCast".

How do I correctly implement the property editor for this property?

0

There are 0 best solutions below