Author: Tomas Rutkauskas
How to use a TProgressbar within a TListbox
Answer:
It is possible, but you have to tie the TProgressBar to the listbox at runtime, the
listbox will not accept a component dropped on it as child. Create a new project,
drop a TButton, TListbox, TProgressbar, TTimer on it. Set the timers Interval to
100, its Enabled property to false. Attach handlers to the Timers OnTimer and
buttons onClick event, complete as below:
unit Unit1;
1 interface
2
3 uses
4 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
5 Forms, Dialogs, stdctrls, ExtCtrls, ComCtrls;
6
7 type
8 TForm1 = class(TForm)
9 Button1: TButton;
10 ListBox1: TListBox;
11 ProgressBar1: TProgressBar;
12 Timer1: TTimer;
13 procedure Timer1Timer(Sender: TObject);
14 procedure Button1Click(Sender: TObject);
15 private
16 { Private declarations }
17 public
18 { Public declarations }
19 end;
20
21 var
22 Form1: TForm1;
23
24 implementation
25
26 {$R *.dfm}
27
28 procedure TForm1.Timer1Timer(Sender: TObject);
29 begin
30 progressbar1.StepIt;
31 if progressbar1.Position >= progressbar1.Max then
32 timer1.enabled := false;
33 end;
34
35 procedure TForm1.Button1Click(Sender: TObject);
36 var
37 i: integer;
38 begin
39 if timer1.Enabled then
40 exit;
41 listbox1.clear;
42 for i := 1 to 20 do
43 listbox1.Items.add(Format('Item %d', [i]));
44 progressbar1.Position := progressbar1.Min;
45 if progressbar1.Parent <> listbox1 then
46 begin
47 progressbar1.Parent := listbox1;
48 progressbar1.BoundsRect := listbox1.ItemRect(2);
49 end;
50 timer1.enabled := true;
51 end;
52
53 end.
Build and run, click the button.
|