Solved

# progress bar

Posted on 2013-05-23
423 Views
I am using the following code for my progress bar.  It increments as progress is made but when it gets to the end of the cycle the green progress indicator is not all the way to the end. Why?

Dim percentage As Integer
For i As Integer = 1 to TotalCount

' ... some work ...

percentage = i / TotalCount * 100
ProgressBar1.Value = percentage ' <-- assuming default Min = 0, Max = 100
Application.DoEvents()
Next
0
Question by:vbnetcoder

LVL 25

Expert Comment

ID: 39191027
I'd bet because of rounding. Instead of doing a percent, set Max to whatever the max number of items are and increment value accordingly and see if it works.
Also try outputting the value of percentage to see if it every reaches 100.
0

LVL 1

Expert Comment

ID: 39192130
Not sure but your calculation of....

percentage = i / TotalCount * 100

Should be

percentage = (i / TotalCount) * 100
0

LVL 85

Expert Comment

ID: 39192307
Since division and multiplication are equal in the order of precedence for operators, the equation will be evaluated from left to right.  Your addition of parenthesis is changing nothing but aesthetics!...  =)
0

LVL 1

Expert Comment

ID: 39192453
True.  Sorry.
0

LVL 40

Accepted Solution

Jacques Bourgeois (James Burger) earned 500 total points
ID: 39197883
First of all, why all these percentage manipulations. Simply set the Maximum to TotalCount before going into the loop, and the following will work a little faster because it does not need to multiply and divide:

ProgressBar1.Maximum = TotalCount
For i As Integer = 1 To totalcount

' ... some work ...

ProgressBar1.Value = i
Application.DoEvents()
Next

Anyway, I tested your code as is, and it works flawlessly. The Progress Bar is filled when finished, although the last block might end up being smaller than the other ones because the width of the control might not leave enough space.

I suspect that your problem might be in   ' ... some work ...

Have you checked to make sure that after the loop. ProgressBar1.Maximum and percentage are at the same value? Is it possible that there is an Exit For somewhere in the loop?
0

Author Closing Comment

ID: 39263318
ty
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…