2008. augusztus 4., hétfő

How to pick from a list of TPanels in a TListBox and display the selected panel


Problem/Question/Abstract:

How to pick from a list of TPanels in a TListBox and display the selected panel

Answer:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure ListBox1Click(Sender: TObject);
  private
    { Private declarations }
    FPanelList: TList;
    FActivePanel: TPanel;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{TForm1}

constructor TForm1.Create(AOwner: TComponent);
var
  i: integer;
  TempPanel: TPanel;
begin
  inherited;
  FPanelList := TList.Create;
  for i := 0 to 20 do
  begin
    TempPanel := TPanel.Create(self);
    TempPanel.Caption := 'TPanel' + IntToStr(i);
    Listbox1.Items.Add(TempPanel.Caption);
    FPanelList.Add(TempPanel);
  end;
end;

destructor TForm1.Destroy;
var
  i: integer;
begin
  for i := FPanelList.Count - 1 downto 0 do
    TPanel(FPanelList[i]).Free;
  FPanelList.Free;
  inherited;
end;

procedure TForm1.ListBox1Click(Sender: TObject);
begin
  if FActivePanel <> nil then
    FActivePanel.Parent := nil;
  FActivePanel := FPanelList[ListBox1.ItemIndex];
  FActivePanel.Parent := self;
end;

end.

Nincsenek megjegyzések:

Megjegyzés küldése