views:

585

answers:

3

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?

+3  A: 

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.

Roee Adler
yes, something like that.if i use a button to begin the process, how am i suppose to do?
Otip88
I decode yor code tobeginlabel1.caption :='';for i := 1 to 1000 do begin if i mod 10 = 0 then label1.caption := label1.caption + '|'; label1.repaint();end;end;but it appear that the "|" shows to fast..
Otip88
Go to the "OnClick" event of the button by double clicking the "OnClick" in the relevant pane. It appears fast probably because your processing is fast (good for you!)... If your whole process takes a second, the bars will appear fast. If you want them to appear slower you acn show a bar only once every 100 ("i mod 100 = 0"), then they will appear slower.
Roee Adler
it works!! one last thing how do I add percentage behind "|" start form 0% to 100% just like the real progress bar...
Otip88
To add the % sign: initialize label.caption := '%'; then in the loop add the '|' in front like label.caption := '|' + label.caption;
Ralph Rickenbach
And put the label.repaint into the if statement for better performance.
Ralph Rickenbach
@malach: thanks, changed
Roee Adler
+5  A: 

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;
Bing
yup it IS excactly what i'm talking about!! you rocks!!!
Otip88
Make sure you only Repaint when necessary, as repaint is costly. Investigate if a test like "if X mod 10 = 0 then Label1.Repaint" would enhance performance.
Ralph Rickenbach
+1  A: 

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-

Neftalí