Question

so here the logic

for 1%="|" in the TLabel and for one "|" we need 10 times looping

so to reach 100%= 100 times "|" we need 1000 times looping

can you help me with the code?

Was it helpful?

Solution

Perhaps you could use the StringOfChar function?

Something like this:


    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;

OTHER TIPS

I'm not 100% sure I get what you mean, but I think it's something like this (assume "label" is 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 the repaint vs. refresh, and whether you should repaint/refresh the entire form, but that's up to you.

Hope that helps.

And this is a variant o Bing solution, that show 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. Regards.


Neftalí -Germán Estévez-

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top