Board index » delphi » TCollection and TCollectionItem

TCollection and TCollectionItem

Can someone help me fix this?

It compiles, but the i try to access it they the onject inspector, i get an
error saying

"Invalid selection": TTestCollection does not have any owner

Here the code.. if it may help... (ive made this code based on the TDBGrid).

----------------------------------------------------------------------------
--
unit Test1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TTestCollectionItem = Class;
  TTest1 = Class;

  TTestCollectionItem = Class(TCollectionItem)
  Private
    FTest: String;
  Protected
  Public
    Constructor Create(Collection: TCollection); Override;
    Destructor Destroy; Override;
  Published
    Property Test: String Read FTest Write FTest;
  End;

  TTestClass = Class Of TTestCollectionItem;

  TTestCollection = Class(TCollection)
  Private
    FTest: TTest1;
  Protected
  Public
    Constructor Create(Test: TTest1; TestClass: TTestClass);
    Destructor Destroy; Override;
  Published
    Property Test: TTest1 Read FTest;
  End;

  TTest1 = class(TWinControl)
  private
    FColl1: TTestCollection;
    procedure SetColl1(const Value: TTestCollection);
    { Dclarations prives}
  protected
    { Dclarations protges}
  public
    { Dclarations publiques}
    Constructor Create(AOwner: TComponent); override;
    Destructor destroy; override;
  published
    Property Collection1: TTestCollection Read FColl1 Write SetColl1;
    { Dclarations publies}
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Exemples', [TTest1]);
end;

{ TTestCollectionItem }

constructor TTestCollectionItem.Create(Collection: TCollection);
begin
  Inherited Create(Collection);
end;

destructor TTestCollectionItem.Destroy;
begin
  Inherited Destroy;
end;

{ TTestCollection }

constructor TTestCollection.Create(Test: TTest1; TestClass: TTestClass);
begin
  Inherited Create(TestClass);
  FTest := Test;
end;

destructor TTestCollection.Destroy;
begin
  Inherited Destroy;
end;

{ TTest1 }

constructor TTest1.Create(AOwner: TComponent);
begin
  Inherited Create(AOwner);
  Width  := 30;
  Height := 30;
  FColl1 := TTestCollection.Create(Self, TTestCollectionItem);
end;

destructor TTest1.destroy;
begin
  Collection1.Free;
  Inherited Destroy;
end;

procedure TTest1.SetColl1(const Value: TTestCollection);
begin
  Collection1.Assign(Value);
end;

end.

-----------------------------------------------------------
Thanks for any answers

Mike

 

Re:TCollection and TCollectionItem


In article <AexQ6.3348$Ui.100...@news.globetrotter.net>, "Michel Hudon"

Quote
<kai...@hotmail.NO.SPAM.com> writes:
>Can someone help me fix this?

>It compiles, but the i try to access it they the onject inspector, i get an
>error saying

>"Invalid selection": TTestCollection does not have any owner

TCollection and TCollectionItem is the "skeleton" and "glue" which builds the
collection and relates the items to the collection. The ItemClass of the
collection Create is specified by the collection in its Add method to create a
new item. The Collection of the TCollectionItem's Create is the Owner of any
newly created collection-item.

If there are any other parameters or action to be taken when a new
collection-item is created, then you must do it either in an overriden Create
of the collection-item, or in a replacement Add (which also calls the inherited
Add) of the collection. Of course a TWinControl must have a Parent and must
have it size set (with SetBounds()).

In any case it is worth replacing the Add method and the Items property ( with
inherited calls) to typecast the returned item to your particular
collection-item class.

Alan Lloyd
alangll...@aol.com

Other Threads