Einzelnen Beitrag anzeigen

Alter Mann

Registriert seit: 15. Nov 2003
Ort: Berlin
946 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#41

AW: TObjectList sehr anfällig?

  Alt 27. Aug 2012, 20:33
Hi,

ist so etwas gemeint?

Delphi-Quellcode:
unit frmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, Spin, uButtons;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    sedtBtnCount: TSpinEdit;
    btnCreate: TButton;
    gbButtons: TGroupBox;
    ButtonPanel: TPanel;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnCreateClick(Sender: TObject);
  private
    { Private-Deklarationen }
    FMyButtonList : TMyButtonList;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.FormCreate(Sender: TObject);
begin
  FMyButtonList := TMyButtonList.Create;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FMyButtonList.Free;
end;

procedure TForm1.btnCreateClick(Sender: TObject);
var
  I, L : Integer;
  MB : TMyButton;
begin
  L := 2;
  for I := FMyButtonList.Count to sedtBtnCount.Value -1 do
  begin
    MB := TMyButton.Create(ButtonPanel);
    MB.Width := 50;
    MB.Height := 25;
    MB.Left := L + (I * 50);
    MB.Top := 2;
    MB.Parent := ButtonPanel;
    MB.Index := FMyButtonList.Add(MB);
  end;
end;

end.
Und

Delphi-Quellcode:
unit uButtons;

interface

uses
  Classes, StdCtrls, Contnrs;

type
  TMyButtonList = class(TObjectList)
  public
    function Add(AObject: TObject): Integer;
  end;

  TMyButton = class(TButton)
  private
    FIndex : Integer;
    procedure SetIndex(Value : Integer);
  public
    constructor Create(aOwner : TComponent); override;
    property Index : Integer read FIndex write SetIndex;
  end;

implementation

uses
  SysUtils;

constructor TMyButton.Create(aOwner : TComponent);
begin
  inherited Create(aOwner);
  Findex := -1;
end;

procedure TMyButton.SetIndex(Value : Integer);
begin
  if Findex <> Value then Findex := Value;
end;

function TMyButtonList.Add(AObject: TObject): Integer;
begin
  if IndexOf(Aobject) <> -1 then Result := IndexOf(Aobject)
  else
  begin
    Result := inherited Add(AObject);
    if (AObject is TMyButton) then
      (AObject as TMyButton).Caption := Format('Knopf %d', [Result + 1]);
  end;
end;

end.
Angehängte Dateien
Dateityp: zip ButtonObjectList.zip (4,4 KB, 1x aufgerufen)
  Mit Zitat antworten Zitat