1 unit GenericCheckList;
2 
3 {$mode objfpc}{$H+}
4 
5 interface
6 
7 uses
8   Classes, SysUtils,
9   Forms, Controls, StdCtrls, Dialogs, ButtonPanel, CheckLst, Buttons,
10   // IdeIntf
11   IDEImagesIntf;
12 
13 type
14 
15   { TGenericCheckListForm }
16 
17   TGenericCheckListForm = class(TForm)
18     ButtonPanel1: TButtonPanel;
19     CheckListBox1: TCheckListBox;
20     InfoLabel: TLabel;
21     procedure CheckListBox1ItemClick(Sender: TObject; {%H-}Index: integer);
22     procedure FormShow(Sender: TObject);
23   private
24     fActionBtn: TBitBtn;
25     procedure UpdateButtons;
26   public
27     constructor Create(TheOwner: TComponent); override;
28     constructor CreateWithActionButton(aCaption: TCaption; aResourceGlyphName: string = '');
29     destructor Destroy; override;
30   end;
31 
32 var
33   GenericCheckListForm: TGenericCheckListForm;
34 
35 implementation
36 
37 {$R *.lfm}
38 
39 { TGenericCheckListForm }
40 
41 constructor TGenericCheckListForm.Create(TheOwner: TComponent);
42 begin
43   inherited Create(TheOwner);
44   InfoLabel.Caption := '';
45 end;
46 
47 constructor TGenericCheckListForm.CreateWithActionButton(aCaption: TCaption;
48   aResourceGlyphName: string);
49 begin
50   Create(Nil);
51   fActionBtn := TBitBtn.Create(ButtonPanel1);
52   fActionBtn.Caption := aCaption;
53   fActionBtn.ModalResult := mrYes;             // ActionButton will return mrYes.
54   fActionBtn.Align := alRight;
55   fActionBtn.BorderSpacing.Left := 6;
56   fActionBtn.BorderSpacing.Right := 6;
57   if aResourceGlyphName <> '' then
58     IDEImages.AssignImage(fActionBtn, aResourceGlyphName);
59   fActionBtn.AutoSize := True;
60   fActionBtn.Parent := ButtonPanel1;
61 end;
62 
63 destructor TGenericCheckListForm.Destroy;
64 begin
65   inherited Destroy;
66 end;
67 
68 procedure TGenericCheckListForm.FormShow(Sender: TObject);
69 begin
70   UpdateButtons;
71 end;
72 
73 procedure TGenericCheckListForm.CheckListBox1ItemClick(Sender: TObject; Index: integer);
74 begin
75   UpdateButtons;
76 end;
77 
78 procedure TGenericCheckListForm.UpdateButtons;
79 var
80   i: Integer;
81 begin
82   if Assigned(fActionBtn) then
83   begin
84     for i := 0 to CheckListBox1.Count-1 do
85       if CheckListBox1.Checked[i] then
86       begin
87         fActionBtn.Enabled := True;
88         Exit;
89       end;
90     fActionBtn.Enabled := False;
91   end;
92 end;
93 
94 end.
95 
96