VB.NET 进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14217894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:33:20  来源:igfitidea点击:

VB.NET Progress Bar

vb.netfor-loopprogress-bar

提问by Alex

Possible Duplicate:
.NET progressbar not updating

可能重复:
.NET 进度条未更新

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

我构建了一个进度条类,用于显示我的 for 循环中的进度。这是进度条类的代码:

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:

这是我在 for 循环中使用它的方法:

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.

问题是视觉问题。它最多完成整个进度条的 70-85%,然后关闭。关闭时,进度条值和最大值相等,但进度条仅填充到其长度的四分之三左右。

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.

我尝试使用 progressBar.Refresh() 而不是 Application.DoEvents() 但它大大减慢了进度 - 并且仍然给我相同的结果。

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

有没有其他方法可以实现更好的视觉吸引力的进度条?

回答by Neolisk

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:

在 Windows 7 上看到这种效果是正常的。问题是当你将一个值设置为 X 时,它会在接下来的 0.5-1 秒内滑动到这个位置。因此,如果您的操作发生得很快,您只会在 50-80% 时看到它。解决方案是先设置一个更高的值,然后再减少到你想要的值。像这样的东西:

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

并且不要忘记临时增加最大值,否则您可能会遇到异常,例如,当从 4999 增加到 5000 时(您不能设置为 5001)。