Einzelnen Beitrag anzeigen

backdraft

Registriert seit: 19. Apr 2005
Ort: Hückeswagen
333 Beiträge
 
Delphi 11 Alexandria
 
#5

Re: Combobox mit AddObject - Speicher automatisch freigeben

  Alt 20. Feb 2007, 11:29
Ok, hab alles aus den Klassen mal in ein Beispielprog geschrieben

Form enthält nur 2 Buttons, die Combo wird angelegt
Nach Klick auf den Hinzufügen button wird beim Prog Ende kein Speicher freigegeben.

Delphi-Quellcode:
unit Unit1;

interface

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


type

  TNeueComboBox = class;

  TNeuesComboBoxItem = class
  protected
    FOwner: TNeueComboBox;
    FItemIndex: Integer;
    FListID : Integer;
    FIndex: Integer;
    FIndentLevel: Integer;
    FImageIndex: Integer;
    FOverlayIndex: Integer;
    FCaption: string;
    FTag: Integer;
    FData: Pointer;
    procedure SetIndentLevel( Value: Integer );
    procedure SetImageIndex( Value: Integer );
    procedure SetCaption( const Value: string );
    procedure SetOverlayIndex( Value: Integer );
  public
    constructor Create( AOwner: TNeueComboBox );
    destructor Destroy; override;
    property Index: Integer read FIndex;
    property IndentLevel: Integer read FIndentLevel write SetIndentLevel;
    property ImageIndex: Integer read FImageIndex write SetImageIndex;
    property OverlayIndex: Integer read FOverlayIndex write SetOverlayIndex;
    property Caption: string read FCaption write SetCaption;
    property Data: Pointer read FData write FData;
    property Tag: Integer read FTag write FTag;
    property ListID: Integer read FListID write FListID;
  end;

   TNeueComboBox = class(TCustomComboBox)
   public
    destructor Destroy; override;
    function AddItem(Caption: string; ImageIndex: Integer; IndentLevel: Integer; ListID: Integer ): TNeuesComboBoxItem;
   end;

type
  TForm1 = class(TForm)
    btn_Hinzu: TButton;
    btn_Delete: TButton;
    procedure FormCreate(Sender: TObject);
    procedure btn_HinzuClick(Sender: TObject);
    procedure btn_DeleteClick(Sender: TObject);
  private
   FComboBox : TNeueComboBox;
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

constructor TNeuesComboBoxItem.Create( AOwner: TNeueComboBox );
begin
  inherited Create;
  FOwner := aOwner;
  FOverlayIndex := -1;
end;

destructor TNeuesComboBoxItem.Destroy;
begin
  inherited;
end;

procedure TNeuesComboBoxItem.SetIndentLevel( Value: Integer );
begin
  FIndentLevel := Value;
  FOwner.Invalidate;
end;

procedure TNeuesComboBoxItem.SetImageIndex( Value: Integer );
begin
  FImageIndex := Value;
  FOwner.Invalidate;
end;

procedure TNeuesComboBoxItem.SetCaption( const Value: string );
begin
  FCaption := Value;
  FOwner.Invalidate;
end;

procedure TNeuesComboBoxItem.SetOverlayIndex( Value: Integer );
begin
  FOverlayIndex := Value;
  FOwner.Invalidate;
end;

function TNeueComboBox.AddItem( Caption: string; ImageIndex: Integer; IndentLevel: Integer; ListID: Integer ): TNeuesComboBoxItem;
const NOSTRING: string = '';
begin
  Result := TNeuesComboBoxItem.Create(Self);
  Result.FCaption := Caption;
  Result.FImageIndex := ImageIndex;
  Result.FIndentLevel := IndentLevel;
  Result.ListID := ListID;
  Result.FIndex := inherited Items.AddObject( Caption, Result )
end;

destructor TNeueComboBox.Destroy;
begin
 WHILE Items.Count > 0 DO Items.Delete(0);
 inherited;
end;

procedure TForm1.btn_DeleteClick(Sender: TObject);
begin
 FComboBox.Items.Clear;
end;

procedure TForm1.btn_HinzuClick(Sender: TObject);
begin
 FComboBox.AddItem('Name', 0, 0, 0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 FComboBox := TNeueComboBox.Create(self);
 FComboBox.Parent := self;
end;

end.
Oliver
  Mit Zitat antworten Zitat