How to make a progress bar in Turbo Delphi using ONLY TLabel NOT TProgressBar

so the logic

for 1% = "|" in TLabel and for one "|" we need 10x cycling

to reach 100% = 100 times "|" we need a loop 1000 times

Can you help me with the code?

0


a source to share


3 answers


Perhaps you could use the StringOfChar function?

Something like that:




    procedure TForm1.Button1Click(Sender: TObject);
    var
      X: Integer;
      Total: Integer;
      Percent: Integer;
    begin
      Total := 1000;
      for X := 1 to Total do
      begin
        Sleep(100);
        Percent := (X * 100) div Total;
        Label1.Caption := StringOfChar('|', Percent) + IntToStr(Percent) + '%';
        Label1.Repaint;
      end;
    end;

      

+6


a source


I'm not 100% sure, I understand what you mean, but I think it is something like this (assume "label" is a TLabel):

label.caption := '';

for i := 1 to 1000 do
begin
    ... do stuff ...
    if i mod 10 = 0 then 
    begin
        label.caption = label.caption + '|';
        label.repaint();
    end;
end;

      



I'm not sure about redrawing and updating, and whether the whole form needs to be redrawn / updated, but that is up to you.

Hope it helps.

+3


a source


And this is a variation of Bing's solution showing the percentage inside (middle) of the bar.

procedure TForm1.Button1Click(Sender: TObject);
var
  X: Integer;
  Total: Integer;
  Percent: Integer;
begin
  Total := 1000;
  for X := 1 to Total do begin
    Sleep(5);
    Percent := (X * 100) div Total;
    Label1.Caption := StringOfChar('|', Percent DIV 2) +
                      ' ' + IntToStr(Percent) + '% ' +
                      StringOfChar('|', Percent DIV 2);
    Label1.Repaint;

    Application.ProcessMessages;

  end;
end;

      

Excuse me for my bad english. Best wishes.


Neftali-Herman Esteves -

+1


a source







All Articles