Einzelnen Beitrag anzeigen

Sora

Registriert seit: 13. Sep 2010
6 Beiträge
 
Delphi 7 Personal
 
#1

Komponente mit dynamischem Array und PropertyEditor

  Alt 13. Sep 2010, 17:36
Hallo liebe Delphi-Praxis-Gemeinde,

ich bin neu und hoffe, dass ich das Thema im richtigen Subforum erstelle.

Ich möchte eine Komponente vom Typ tWorld schreiben, die auf die Form im Entwicklungsmodus gezogen werden kann. Diese World soll ein Unterobjekt list vom Typ tComponentList besitzen, wobei tMyComps eine Klasse aus TComponent ist und als Eigenschaft ein dynamisches Array + Zugriffsroutinen enthält. Dieses dynamische Array hat als Feldtyp tComp, ebenfalls aus TComponent mit Id und Name.

Mein Ziel ist es einen PropertyEditor für TComponentList zu schreiben, sodass alle Unterkomponenten ebenfalls gespeichert werden. Das möchte ich vlt. über TFileStream.WriteComponentRes realisieren. Im Editor sollen dann über Buttons (Add, Remove) und eine ComboBox die Unterkomponenten verwaltet werden. Es wäre auch schön, wenn die Unterkomponenten vom Typ tComp als Subkomponenten der World in der Objekt-Hierarchie angezeigt würden. Ich weiß aber nicht, wie das geht. Ich wäre dankbar, wenn mir auch hier geholfen werden könnte.

Leider habe ich ein Problem:

Nachdem ich die Unit UWorld in dclusr70.bpl kompiliert habe und nun meine World-Komponente auf das Formular ziehen will, kommt eine Fehlermeldung à la Zugriffsverletzung bei Adresse 510069D0 in Modul 'dclusr70.bpl'. Lesen von Adresse 00000000.

Ich hoffe, dass ihr mir helfen könnt. Wahrscheinlich bin ich nur unsagbar blöd.

Meine Quelltexte:

UWorld:
Delphi-Quellcode:
unit UWorld;

interface

uses
  SysUtils, Classes, UDataTypes;

type
  TWorld = class(TComponent)
  private
  protected
    { Protected-Deklarationen }
  public
    list: tComponentList;
    constructor Create(AOwner: TComponent); override;
  published
    { Published-Deklarationen }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Beispiele', [TWorld]);
end;

constructor TWorld.Create(AOwner: TComponent);
begin
  inherited;
  Self.list.Create(Self);
end;

end.
UDataTypes:

Delphi-Quellcode:
unit UDataTypes;

interface
uses Classes;

type
  tComp = class(TComponent)
    private
      Id: word;
      Name: String;
    public
      constructor Create(anId: word; aName: String); overload;
  end;

  tComponentList = class(TComponent)
    private
      MyComps: array of tComp;
      function IssetComp(CompId : Word) : BOOLEAN;
    public
      function GetCompById(CompId : WORD) : tComp;
      function GetCompByName(CompName : String) : tComp;
      procedure AddComp(AComp : tComp);
      procedure DelComp(CompId : WORD);
      function CompCount: integer;
  end;

implementation

{##############################################################################}
{############################ tMyComp-Routinen ################################}
{##############################################################################}

constructor tComp.Create(anId: word; aName: String);
begin
  Self.ID:=anId;
  Self.Name:=aName;
end;

{##############################################################################}
{########################### tComponentList-Routinen ##########################}
{##############################################################################}

procedure TComponentList.AddComp(AComp: tComp);
begin
  SetLength(Self.MyComps,Length(Self.MyComps)+1);
  MyComps[High(MyComps)]:=AComp;
  Self.Owner.InsertComponent(AComp);
end;

procedure TComponentList.DelComp(CompId: word);
var i: integer;
begin
  Self.Owner.RemoveComponent(Self.GetCompById(CompId));
  for i:=CompId to CompCount-2 do
  begin
    Self.MyComps[i].Free;
    Self.MyComps[i]:=tComp.Create(Self.GetCompById(i+1).Id,Self.GetCompById(i+1).Name);
  end;
  Self.MyComps[CompCount-1].Free;
  SetLength(Self.MyComps,CompCount-1);
end;

function TComponentList.IssetComp(CompId: word): Boolean;
var
  i : word;
  tmpBool : boolean;
BEGIN
  tmpBool:=false;
  if(Self.CompCount <> 0) THEN
  Begin
    i := 0;
    repeat
      if(Self.MyComps[i].Id = CompId) THEN tmpBool:=TRUE;
      inc(i);
    until ((i>=Self.CompCount-1) OR (tmpBool = TRUE));
   End;

  IssetComp:=tmpBool;
END;

function TComponentList.CompCount: Integer;
begin
  Result:=Length(Self.MyComps);
end;

function TComponentList.GetCompById(CompId: word): tComp;
begin
  if( IssetComp(CompId) ) THEN
    GetCompById:=Self.MyComps[CompId]
  else
    GetCompById:=nil;
end;

function TComponentList.GetCompByName(CompName: String): tComp;
VAR
  i : WORD;
  tmpBool : BOOLEAN;
  tmpComp: tComp;
BEGIN
  tmpBool:=FALSE;
  tmpComp:=nil;
  if(Self.CompCount <> 0) THEN
  Begin
    i := 0;
    repeat
      if(Self.MyComps[i].Name = CompName) THEN
      Begin
        tmpBool:=TRUE;
        tmpComp := Self.MyComps[i];
      End
      else tmpComp := nil;
      inc(i);
    until ((i>=CompCount-1) OR (tmpBool = TRUE));
   End;
   GetCompByName:=tmpComp;
END;

end.
Liebe Grüße,

Sora

Geändert von Sora (13. Sep 2010 um 17:45 Uhr)
  Mit Zitat antworten Zitat