Einzelnen Beitrag anzeigen

Sora

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

AW: Komponente mit dynamischem Array und PropertyEditor

  Alt 14. Sep 2010, 05:14
Vielen Dank für die schnelle Antwort!

Ich habe diesen dummen (und er ist wirklich dumm, aber ich hab ihn nicht gefunden...) Fehler beseitigt und noch Getter- und Setter-Methoden für tComp und einen Add- und einen Delete-Button auf der Form eingeführt. Adden funktioniert zwar, beim Löschen kommt aber eine Zugriffsverletzung, deren Grund ich nicht verstehe.

UMainform:

Delphi-Quellcode:
unit UMainform;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UWorld, StdCtrls, UDatatypes;

type
  TForm1 = class(TForm)
    ButtonAdd: TButton;
    ButtonDelete: TButton;
    LabelCount: TLabel;
    World1: TWorld;
    procedure ButtonAddClick(Sender: TObject);
    procedure ButtonDeleteClick(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ButtonAddClick(Sender: TObject);
begin
  World1.List.AddComp(tComp.Create(0,''));
  LabelCount.Caption:=inttostr(World1.List.CompCount);
end;

procedure TForm1.ButtonDeleteClick(Sender: TObject);
begin
  World1.List.DelComp(0);
  LabelCount.Caption:=inttostr(World1.List.CompCount);
end;

end.
UDataTypes:

Delphi-Quellcode:
unit UDataTypes;

interface
uses Classes;

type
  tComp = class(TComponent)
    private
      Id: word;
      Name: String;
    public
      function GetId: Word;
      procedure SetId(Value: Word);
      function GetName: String;
      procedure SetName(Value: String);
      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.SetId(anId);
  Self.SetName(aName);
end;

function tComp.GetId: Word;
begin
  GetId:=Self.Id;
end;

procedure tComp.SetId(Value: Word);
begin
  Self.Id:=Value;
end;

function tComp.GetName: String;
begin
  GetName:=Self.Name;
end;

procedure tComp.SetName(Value: String);
begin
  Self.Name:=Value;
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
  if (IssetComp(CompId)) then
  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).GetId,Self.GetCompById(i+1).GetName);
    end;
    Self.MyComps[CompCount-1].Free;
    SetLength(Self.MyComps,CompCount-1);
  end;
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].GetId = 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].GetName = 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.
UWorld:

Delphi-Quellcode:
unit UWorld;

interface

uses
  SysUtils, Classes, UDataTypes;

type
  TWorld = class(TComponent)
  private
    FList: tComponentList;
  protected
    { Protected-Deklarationen }
  public
    property List: tComponentList read FList write FList;
    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;
  List:=tComponentList.Create(Self);
end;

end.
Ich hoffe, dass ihr mir hier noch mal helft.

Liebe Grüße,

Sora
  Mit Zitat antworten Zitat