Reputation: 3057
I'm trying to make a progress bar that starts at 0%, and takes 5 seconds to get to 100%. The progress bar will begin to go up as soon as Button1 is clicked. Any advice? I looked on Google, but that gave me nothing good on this sort of thing.
Also, at 0%, there should be a label that says Waiting...
, when the progress bar starts, it should go to Working...
, and when it's done, it should say Done!
.
Upvotes: 3
Views: 26081
Reputation: 23289
Using GetTickCount() and initializing variables:
uses Windows;
var mseconds, starttime: integer;
procedore TForm1.FormCreate()
begin
starttime := GetTickCount();
mseconds := 0;
Timer1.Enabled := false;
Label1.Caption := '';
ProgressBar1.Position := 0;
Label1.Caption := 'Waiting...';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ProgressBar1.Min := 0;
ProgressBar.Max := 100;
ProgressBar1.Position := 0;
timer1.Enabled := True;
Label1.Caption := 'Working...';
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
mseconds := GetTickCount() - starttime;
if mseconds < 5000 then
ProgressBar1.Position := Trunc(mseconds / 50)
else begin
ProgressBar1.Position := 100;
Label1.Caption := 'Done!';
Timer1.Enabled := false;
end;
end;
Upvotes: 6
Reputation: 9294
You can use a timer with interval 50 and firstly set enabled to false.
procedure TForm1.Button1Click(Sender: TObject);
begin
timer1.Enabled := True;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
const
cnt: integer = 1;
begin
ProgressBar1.Position := cnt;
if cnt = 1 then Label1.Caption := 'Waiting...'
else if cnt = 100 then begin
Label1.Caption := 'Done!';
Timer1.Enabled := False;
end else
Label1.Caption := 'Working...';
Inc(cnt);
end;
Upvotes: 9