Using Windows Forms progress bar - windows forms app

I have progress bar on my applications.  It should update as a for next loop progresses.

How do i do it?
vbnetcoderAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Something like...
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

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you're running in the main UI thread:
Some Loop
    ... code ...
    ProgressBar1.Value = x
    Application.DoEvents()
End Loop

Open in new window


You should move the code to a background thread though.  Again, take a look at the BackgroundWorker() control.  It has a ReportProgress() method which fires the ProgressChanged() event.  Search for it...examples abound!
0
 
vbnetcoderAuthor Commented:
how will this code know what percentage of the total was just completed.

For example if the loop is going to be 10 times each time i want to move the bar by a tenth
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Since you know how many times your loop is going to iterate, just divide the current iteration  number by the total number and multiply by 100 (a percentage!).  Then leave your progressbar at the default maximum of 100 and set it to the current "percentage" value.

Another option is to set the Maximum property of the progressbar to your desired number then call PerformStep() against it for each one (set the Step to 1).
0
 
vbnetcoderAuthor Commented:
I not sure i am following you...


I have a variable name TotalCount that is the total amount of time it will loop


What would the code look like?
0
 
vbnetcoderAuthor Commented:
ty
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.