Delphi progress bar

I am trying to make a progress bar that starts at 0% and takes 5 seconds to get to 100%. The progress bar will begin to grow as soon as Button1 clicks. Any advice? I looked at Google, but it didn’t help me with anything good.

In addition, at 0% there should be a label with the inscription Waiting..., when the progress bar starts, it should go to Working..., and when it is done, it should say Done!.

+3
source share
2 answers

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;
+5
source

You can use a timer at intervals of 50 and set it to false first.

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
source

Source: https://habr.com/ru/post/1705549/


All Articles