tags:

views:

91

answers:

3

I have one progressbar and an image on my form. Now I am doing some processing on each scanline on image. I wanted to show progress of my process using progressbar. I set the following properties of progressbar :

Max = 20
Min = 0
Position = 0
Step = 1
Smooth = False

Now my code on buttonclick event is as under :

   stat := imgmain.Picture.Bitmap.Height div pbImage.Max;
   Curstatus := 0;
   for cnt := 0 to img.Picture.Bitmap.Height - 1 do begin
      for cnt1 := 0 to img.Picture.Bitmap.Width - 1 do begin
        //Some processing
      end;
      if cnt > (Curstatus + stat) then begin
         pgimg.StepIt;
         Curstatus := cnt + stat;
      end;    
   end;

My problem is when I run this code, the if block is executed exactly 20 times but it shows more than 20 steps on my progress bar. Also the last step is shown incomplete. Note that smooth property is set to false.

+2  A: 

With

Max = 20 
Min = 0 

You actually have 21 steps in your progress bar, with 0 being the first and 20 being the last. Set Max to 19, and it should work.

Nick
@Nick I change this values and now `if block` is executed 19 times. But the problem is same, It will not make any difference because the value of `stat` is calculated using max value of progressbar.
Himadri
@Nick @DwrCymru My code is working fine, It fills whole progressbar when process is completed, the problem with my code, your suggested code and even answered by DwrCymru is that it is not showing the number of blocks as specified by `Max` property. i.e., max = 19 but it is showing 24 blocks , max = 768 but showing 27 blocks
Himadri
+1  A: 

Set the Max value to the height of your image and increase position after processing (or before) each scan line.

The only progress bar I know of that will behave the way you want is the "TJvXPProgressBar" (part of JEDI VCL here) which will increase the number of blocks dsiplayed when Position is increased so with Max := 20, Min := 1 and Position := 5 there will be 5 blocks displayed.

DwrCymru
+1  A: 

Min, Max and Position are used so the Progressbar can automatically how much work has been completed. Example:

Min := 100;
Max := 150;
Position := 125;
=> 50% work complete, the progress bar will be half-filled with green blocks.

Step is used so the progress bar knows how much to add to "Position" when you call StepIt. For example, if you'd do a progress bar for a file-copy operation, you might use something like this:

Min := 0;
Max := 2 * 1024 * 1024; // 2Mb
Step := 4 * 1024; // 4Kb
Position := 0;

// in a loop, copy 4Kb blocks, and then do:
ProgressBar.StepIt; // this is equivalent to Position := Position + Step;

Min and Max have nothing to do with the number of blocks you see on screen, that's entirely implementation-specific. For example Windows versions before XP didn't show blocks, they showed a simple filled bar. The size of the blocks might even be theme-dependent! Windows will show sufficient blocks to express the percentage of complete work. Min, Max and Position are used to calculate that percentage.

Cosmin Prund