Progressbar inside Button? No problem
It’s quite cool efect, but this is also effective way to save some space in your application GUI. Let’s place progressbar of some time-consuming function right into the button, which started this function.
Place Button and Progressbar components on a form and set Visible property of Progressbar to False. Now, use the following code to do the magic.
When you press the Button, the Progressbar will be set to visible (disabling the button itself). When the time-consuming function is over, the button will be reenabled again and progressbar will be set to invisible state.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls; type TForm1 = class(TForm) Button1: TButton; ProgressBar1: TProgressBar; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin ProgressBar1.Parent := Button1; ProgressBar1.Width := Button1.Width - 7; ProgressBar1.Height := Button1.Height - 7; ProgressBar1.Left := 3; ProgressBar1.Top := 3; end; procedure TForm1.Button1Click(Sender: TObject); var i : byte; begin Button1.Enabled := false; if ProgressBar1.Visible then Exit; ProgressBar1.Position := 0; ProgressBar1.Visible := True; //replace with real code----------- for i := 0 to 99 do begin Sleep(100); ProgressBar1.Position := ProgressBar1.Position + 1; Application.ProcessMessages; end; //--------------------------------- ProgressBar1.Visible := False; Button1.Enabled := true; end; end. |