3

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!.

2 Answers 2

9

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;
9
  • One problem with using timers is that there is no guarantee it will take 5 seconds (2.5 in your example btw). Most of the times it will be a very close match. In a stressfull environment, it could be of by a magnitude of seconds. Mar 30, 2009 at 7:15
  • @Lieven: You are right, but the influence of this can be minimized by a less dumb algorithm: don't count the timer ticks, but calculate the elapsed time since the start in the event handler. For higher message priority post a WM_USER each 100 ms from a background thread instead.
    – mghie
    Mar 30, 2009 at 10:48
  • Yep. This isn't a perfect solution, but the basic idea is correct Mar 30, 2009 at 11:54
  • Ok, I tried your code, and I run into a few problems. On my form, I have a TButton, TProgressBar, TTimer, and a TLabel. I copy and pasted the code, but it says that "Timer does not contain a member 'Enabled'". Same goes with Tprogressbar.Position, and Label1.Caption. What could I be missing?
    – PuppyKevin
    Mar 30, 2009 at 13:24
  • Which Delphi version are you using?
    – SimaWB
    Mar 30, 2009 at 13:50
6

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;
3
  • Ok. Your's had a few errors, but I am able to fix them up. For some reason though, when I hit the button, the label changes to Working..., but it does nothing else after that. I looked though to see if anything was wrong, could it be caused by: starttime := GetTickCount(); (read next comment)
    – PuppyKevin
    Mar 30, 2009 at 17:43
  • and mseconds := starttime - GetTickCount();?
    – PuppyKevin
    Mar 30, 2009 at 17:44
  • @PuppyKevin: You are on the right track; if you don't see the error then use the debugger, and you will find it. Also, Timer1 is not disabled when done...
    – mghie
    Mar 31, 2009 at 4:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.