Список кнопок
unit BtnsList;
interface
uses
Classes, StdCtrls;
type
TButtonList = class(TObject)
private
FList: TList;
function Get(Index: Integer): TButton;
procedure Put(Index: Integer; Item: TButton);
function GetCount: Integer;
public
constructor Create;
destructor Destroy; override;
function Add(Item: TButton): Integer;
function Equals(List: TButtonList): Boolean;
property Count: Integer
read GetCount;
property Items[Index: Integer]: TButton
read Get write Put; default;
end;
implementation
constructor TButtonList.Create;
begin
inherited Create;
FList := TList.Create;
end;
destructor TButtonList.Destroy;
begin
FList.Free;
inherited Destroy;
end;
function TButtonList.Get(Index: Integer): TButton;
begin
Result := FList[Index];
end;
procedure TButtonList.Put(Index: Integer; Item: TButton);
begin
FList[Index] := Item;
end;
function TButtonList.GetCount: Integer;
begin
Result := FList.Count;
end;
function TButtonList.Add(Item: TButton): Integer;
begin
Result := FList.Add(Item);
end;
function TButtonList.Equals(List: TButtonList): Boolean;
var
I: Integer;
begin
Result := False;
if List.Count <> FList.Count then
Exit;
for I := 0 to List.Count - 1 do
if List[I] <> FList[I] then
Exit;
Result := True;
end;
end.
unit ListForm;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, BtnsList;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
SpeedButton1: TSpeedButton;
ListBox1: TListBox;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure AddToList(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure UpdateList(Sender: TObject);
private
List: TButtonList;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.AddToList(Sender: TObject);
begin
// checks the type
List.Add(Sender as TButton);
// no go:
// List.Add (Sender);
UpdateList(self);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
List := TButtonList.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
List.Free;
end;
procedure TForm1.UpdateList(Sender: TObject);
var
I: Integer;
begin
ListBox1.Items.Clear;
for I := 0 to List.Count - 1 do
ListBox1.Items.Add(Format('%s (%s)',
[List[I].Caption, List[I].ClassName]));
end;
end.
Скачать весь проект
|