Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Progress Bar [duplicate]

Possible Duplicate:
.NET progressbar not updating

I built a progress bar class that shows the progress in my for loops. Here's the code for the progress bar class:

Public Class frmProgress
Private Sub frmProgress_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    progressBar.Minimum = 0
End Sub

Public Sub ProgressBarSetup(ByRef Maximum As Integer, ByRef Title As String)
    progressBar.Maximum = Maximum
    progressBar.Value = 0
    Me.Text = Title
    Me.Show()
End Sub

Public Sub IncProg()
    progressBar.Value += 1
    Application.DoEvents()

    If progressBar.Value = progressBar.Maximum Then
        Me.Close()
    End If
End Sub
End Class

Here's how I would use it in a for loop:

Dim pb As New ProgressBar
pb.ProgressBarSetup(5000, "Test")

For i As Integer = 0 To 5000 - 1
      pb.IncProg()
Next

The issue is a visual problem. It completes up to 70-85% of the complete progress bar and then closes. On closure, the progress bar value and maximum values are equal, yet the bar is only filled to about three quarters of it's length.

I tried using progressBar.Refresh() instead of Application.DoEvents() but it slows down the progress by a lot - and still gives me the same result.

Is there any other ways to achieve a better visually appealing progress bar?

like image 380
Alex Avatar asked Jan 08 '13 15:01

Alex


1 Answers

Seeing this effect is normal on Windows 7. Problem is when you set a value to X, it slides to this position over the next 0.5-1 second. So if your action is happening fast, you will only see it full at 50-80%. Solution is to set first to a higher value and then decrement to the one you want. Something like this:

progressBar.Value += 2
progressBar.Value -= 1

And also don't forget to increase maximum temporarily, or you may get an exception, for example, when incrementing from 4999 to 5000 (you cannot set to 5001).

like image 180
Neolisk Avatar answered Nov 17 '22 20:11

Neolisk