Einzelnen Beitrag anzeigen

Hansa

Registriert seit: 9. Jun 2002
Ort: Saarland
7.554 Beiträge
 
Delphi 8 Professional
 
#100
  Alt 29. Apr 2003, 20:50
Hi,

das Thema geht noch weiter, auch wenn es nur wenige interessiert. Ich habe hier einen Code, den ich aus weltweiten Bestandteilen zusammengestückelt habe. Ich finde, der Code ist sehr einfach, aber effizient.

Dabei geht es allerdings um eine ListBox mit Label:

Delphi-Quellcode:
unit labeledlistbox;

interface

uses Controls, stdctrls, ExtCtrls, Classes;

procedure Register;

implementation

type

  TMyListBox = Class( TCustomPanel )
  private
    FLabel: TLabel;
    FListBox: TListBox;
    procedure SetLabelCaption(const Value: String);
    function GetLabelCaption: String;
    function GetListBoxItems: TStrings;
    function GetListBoxItemIndex: Integer;
    function GetListBoxOnClick: TNotifyEvent;
    procedure SetListBoxItemIndex(const Value: Integer);
    procedure SetListBoxOnCLick(const Value: TNotifyEvent);
  public
    constructor Create( AOwner: TComponent ); override;
    destructor Destroy; override;
  published
    property Font;
    property LabelCaption: String read GetLabelCaption write SetLabelCaption;
    property ListBoxItems: TStrings read GetListBoxItems;
    property ListBoxItemIndex: Integer read GetListBoxItemIndex write SetListBoxItemIndex;
    property ListBoxOnClick: TNotifyEvent read GetListBoxOnClick write SetListBoxOnCLick;
  end;

  constructor TMyListBox.Create(AOwner: TComponent);
  begin
    inherited Create( AOwner );

    FLabel := TLabel.Create( Self );
    FLabel.Parent := Self;
    FLabel.Align := alTop;
    FLabel.Caption := 'LABEL_CAPTION';

    FListBox := TListBox.Create( Self );
    FListBox.PArent := Self;
    FListBox.Align := alClient;
  end;

  destructor TMyListBox.Destroy;
  begin
    FLabel.Free;
    FListBox.Free;

    inherited Destroy;
  end;

  function TMyListBox.GetLabelCaption: String;
  begin
    Result := FLabel.Caption;
  end;

  function TMyListBox.GetListBoxItemIndex: Integer;
  begin
    Result := FListBox.ItemIndex;
  end;

  function TMyListBox.GetListBoxItems: TStrings;
  begin
    Result := FListBox.Items;
  end;

  function TMyListBox.GetListBoxOnClick: TNotifyEvent;
  begin
    Result := FListBox.OnClick;
  end;

  procedure TMyListBox.SetLabelCaption(const Value: String);
  begin
    if FLabel.Caption <> Value then
    begin
      FLabel.Caption := Value;
      Invalidate;
    end;
  end;

  procedure TMyListBox.SetListBoxItemIndex(const Value: Integer);
  begin
    if FListBox.ItemIndex <> Value then
    begin
      FListBox.ItemIndex := Value;
      Invalidate;
    end;
  end;

  procedure TMyListBox.SetListBoxOnCLick(const Value: TNotifyEvent);
  begin
    FListBox.OnClick := Value;
  end;

procedure Register;
begin
  RegisterComponents('Additional', [TMyListBox]);
end;

end.
[edit=Daniel B]Delphi-Tags ergänzt. MfG Daniel B.[/edit]
Gruß
Hansa
  Mit Zitat antworten Zitat