troubleshooting Question

New Collection Componet with error

Avatar of PPaul
PPaul asked on
Delphi
2 Comments1 Solution172 ViewsLast Modified:
I'm making an component that will have a list of another component in it. I've read all the books I have here on the Subject of TCollection and TCollectionItem and I get the following error whe I try to add a item in the Object inspetor in D5

Invalid Selection: 'TSkillItems' has no Owner.

From what I can tell from what I read TSkellItems is not to have a owner, and if it does is it the main Component that it is linked to?

Eather way if anyone can find the problem I'm willing to spend the points.

I hate putting code in my Question but here it is.

unit VampierBox;

interface

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

type
  TVampierBox = Class;

  TSkillItem = Class(TCollectionItem)
  private
    { Private declarations }
     FLeft: Integer;
     FTop: Integer;
     FVampItem: TVampItem;
     FWidth: Integer;
     FHeight: Integer;
     Procedure SetTop(Const Value: Integer);
     Procedure SetLeft(Const Value: Integer);
     Procedure SetWidth(Const Value: Integer);

  public
    { Public declarations }
    Constructor Create(Collection: TCollection); Override;
    Destructor Destroy; Override;
    Procedure Assign(Source: TPersistent); Override;
    //Property Width: Integer Read FWidth:
    Property Height: Integer Read FHeight;

  published
    { Published declarations }
    Property Left: Integer read FLeft Write SetLeft;
    Property Top: integer Read FTop Write SetTop;
    Property Width: Integer Read FWidth Write SetWidth;

  end;

  TSkillItems = class(TCollection)
  private
    { Private declarations }
    FVampier: TVampierBox;
    Function GetItem(Index: Integer): TSkillItem;
    Procedure SetItem(Index: integer; Value: TSkillItem);

  protected
    { Protected declarations }
    Procedure UpDate(Item: TCollectionItem);Override;

  public
    { Public declarations }
    Constructor Create(VampierBox: TVampierBox);
    Function Add: TSkillItem;
    Procedure UpdateSkills;
    Property Items[Index: Integer]: TSkillItem Read GetItem write SetItem; Default;
  end;


  TVampierBox = class(TScrollBox)
  private
    { Private declarations }
    FSkills: TSkillItems;
    TopAlign,LeftAlign,ItemWidth: Integer;
    Procedure SetSkills(Value: TSkillItems);
    Procedure UpdateSkill(Index: Integer);

  public
    { Public declarations }
    Constructor Create(AOwner: TComponent); Override;
    Destructor Destroy; Override;
    Procedure GetChildren(Proc: TGetChildProc; Root: TComponent); Override;

  published
    { Published declarations }
    Property Skills: TSkillItems Read FSkills Write SetSkills;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('PPG', [TVampierBox]);
end;

//////////////////////////////////////////////////////////////////
// TSkillItem
//////////////////////////////////////////////////////////////////

Constructor TSkillItem.Create(Collection: TCollection);
{ This Constructor Gets the TCollection that owns the TTSkillItem }
Begin
     Inherited Create(Collection);
     FVampItem:= TVampItem.Create(TSkillItems(Collection).FVampier);
     FVampItem.Parent := TSkillItems(Collection).FVampier;

     FWidth:= FVampItem.Width;
     FHeight:= FVampItem.Height;
end;

Destructor TSkillItem.Destroy;
Begin
     FVampItem.Free;
     Inherited Destroy;
end;

Procedure TSkillItem.Assign(Source: TPersistent);
Begin
     If Source is TSkillItem Then Begin
          FLeft:=TSkillItem(source).Left;
          FTop:=TSkillItem(Source).Top;
          FWidth:=TSkillItem(Source).Width;
          Exit;
     end;
     Inherited Assign(Source);
End;

Procedure TSkillItem.SetLeft(Const Value: Integer);
Begin
     if FVampItem <> Nil then Begin
          FLeft:= Value;
          FVampItem.Left:=FLeft;
     end;
End;

Procedure TSkillItem.SetTop(Const Value: Integer);
Begin
     if FVampItem <> Nil then Begin
          FTop:= Value;
          FVampItem.Top:=FTop;
     end;
End;

Procedure TSkillItem.SetWidth(Const Value: Integer);
Begin
     if FVampItem <> Nil then Begin
          FWidth:= Value;
          FVampItem.Width:=FWidth;
     end;
End;

//////////////////////////////////////////////////////////////////
// TSkillItems
//////////////////////////////////////////////////////////////////


Constructor TSkillItems.Create(VampierBox: TVampierBox);
begin
     Inherited Create(TSkillItem);

     FVampier := VampierBox;

end;

Function TSkillItems.GetItem(Index: Integer): TSkillItem;
Begin
     Result :=TSkillItem(Inherited GetItem(Index));
end;

Procedure TSkillItems.SetItem(Index: integer; Value: TSkillItem);
Begin
     Inherited SetItem(Index, Value)
end;

Procedure TSkillItems.UpDate(Item: TCollectionItem);
Begin
    If Item <> nil then
    FVampier.UpdateSkill(Item.Index);
end;

Procedure TSkillItems.UpdateSkills;
Var
i: Integer;
Begin
     for i:=0 to count-1 do
     FVampier.UpdateSkill(i);
end;

Function TSkillItems.Add: TSkillItem;
Begin
     Result := TSkillItem(inherited Add);
end;

//////////////////////////////////////////////////////////////////
// TVampierBox
//////////////////////////////////////////////////////////////////

Constructor TVampierBox.Create(AOwner: Tcomponent);
begin
     Inherited Create(AOwner);
     FSkills:=TSkillItems.Create(Self);
     TopAlign :=0;
     LeftAlign :=0;
     ItemWidth := Width;
end;

Destructor TVampierBox.Destroy;
Begin
     FSkills.Free;
     Inherited Destroy;
end;

Procedure TVampierBox.GetChildren(Proc: TGetChildProc; Root: TComponent);
Var
     i:Integer;
Begin
     For i :=0 to ControlCount -1 do
          if not (Controls[i] is TVampItem) then
               Proc(TComponent(Controls[i]));
end;

Procedure TVampierBox.SetSkills(Value: TSkillItems);
Begin
     FSkills.Assign(Value);
end;

Procedure TVampierBox.UpdateSkill(index: Integer);
Begin
     if Index = 0 then Begin
          TopAlign :=0;
          LeftAlign := 0;
          ItemWidth := ClientWidth;
     end
     else Begin
          TopAlign :=TopAlign + FSkills[Index].Height;
          LeftAlign:=0;
          ItemWidth:=ClientWidth;
     end;

     FSkills[Index].Left:= LeftAlign;
     Fskills[Index].Top:= TopAlign;
     FSkills[Index].Width:=ItemWidth;
end;



end.
ASKER CERTIFIED SOLUTION
Cynna

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros