VB.NET Progress Bar [duplicate]

折月煮酒 提交于 2019-12-19 06:33:28

问题


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?


回答1:


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).



来源:https://stackoverflow.com/questions/14217894/vb-net-progress-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!