Показать сообщение отдельно
  #3  
Старый 18.10.2012, 09:08
Аватар для NumLock
NumLock NumLock вне форума
Let Me Show You
 
Регистрация: 30.04.2010
Адрес: Северодвинск
Сообщения: 5,426
Версия Delphi: 7, XE5
Репутация: 59586
По умолчанию

Код:
unit Unit2;

interface

uses
  Classes;

type
  TMyCollectionItem = class(TCollectionItem)
  private
    FId: Integer;
    FName: String;
  public
    constructor Create(Collection: TCollection); override;
  published
    property Id: Integer read FId write FId;
    property Name: String read FName write FName;
  end;

  TMyComponent = class(TComponent)
  private
    FMyCollection: TOwnedCollection; 
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property MyCollection: TOwnedCollection read FMyCollection write FMyCollection;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('My', [TMyComponent]);
end;

{ TMyCollectionItem }

constructor TMyCollectionItem.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  FId:=0;
  FName:='';
end;

{ TMyComponent }

constructor TMyComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FMyCollection:=TOwnedCollection.Create(Self, TMyCollectionItem);
end;

destructor TMyComponent.Destroy;
begin
  FMyCollection.Free;
  inherited Destroy;
end;

end.

но лучше так не делать - не удобно работать с такой коллекцией.

правильней создать свой класс коллекцию, унаследовавшись от TOwnedCollection или TCollection (реализовав GetOwner). задать свой тип у Items, Add, Insert и т.д.
__________________
Пишу программы за еду.
__________________
Ответить с цитированием