Einzelnen Beitrag anzeigen

Benutzerbild von Olli73
Olli73

Registriert seit: 25. Apr 2008
Ort: Neunkirchen
741 Beiträge
 
#2

AW: RAD 11 Delphi - Wie erstelle ich Collections ?

  Alt 15. Sep 2023, 16:24
Hier mal eine nicht visuelle Komponente, die eine Stringlist (hier nicht relevant) und eine Collection von StringListen enthält.

Delphi-Quellcode:
unit StringsContainer;

interface

uses
  Windows, Messages, SysUtils, Classes;

type


  TStringsColItm = class(TCollectionItem)
  private
    FESL: TStringList;
    procedure SetESL(Value: TStringList);
  public
    constructor Create(Collection: TCollection); override;
    destructor Destroy; override;
  published
    property ESL: TStringList read FESL write SetESL;
  end;


  TStringsCol = class(TCollection)
  private
    function GetItem(Idx: Integer): TStringsColItm;
    procedure SetItem(Idx: Integer; Value: TStringsColItm);
  public
    property XItems[Idx: Integer]: TStringsColItm read GetItem write SetItem; default;
  published
  end;



  TStringsContainer = class(TComponent)
  private
    { Private declarations }
    FSL: TStringList;
    FSC: TStringsCol;
    procedure SetSL(Value: TStringList);
    procedure SetSC(Value: TStringsCol);
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Published declarations }
    property SL: TStringList read FSL write SetSL;
    property SC: TStringsCol read FSC write SetSC;
  end;

procedure Register;

implementation


constructor TStringsColItm.create(Collection: TCollection);
begin
  inherited;
  FESL := TStringList.Create;
end;


destructor TStringsColItm.destroy;
begin
  FESL.Free;
  inherited;
end;


function TStringsCol.GetItem(Idx: Integer): TStringsColItm;
begin
  result := TStringsColItm(inherited Items[Idx]);
end;


procedure TStringsCol.SetItem(Idx: Integer; Value: TStringsColItm);
begin
  inherited Items[Idx] := Value;
end;


procedure TStringsContainer.SetSL(Value: TStringList);
begin
  FSL.Assign(Value);
end;


constructor TStringsContainer.Create(AOwner: TComponent);
begin
  inherited;
  FSL := TStringList.Create;
  FSC := TStringsCol.Create(TStringsColItm);
end;


destructor TStringsContainer.destroy;
begin
  FSL.Free;
  FSC.Free;
  inherited;
end;


procedure Register;
begin
  RegisterComponents('MyComps', [TStringsContainer]);
end;

procedure TStringsColItm.SetESL(Value: TStringList);
begin
  FESL.Assign(Value);
end;

procedure TStringsContainer.SetSC(Value: TStringsCol);
begin
  FSC.Assign(Value);
end;

end.
  Mit Zitat antworten Zitat