Winforms ProgressBar Takes time to Render

前端 未结 7 1694
清歌不尽
清歌不尽 2021-01-15 10:12

I have noticied that when using the PorgressBar. If I set the value to x, the value displayed is not immediately updated, it takes a small amount of time to draw it as the b

7条回答
  •  独厮守ぢ
    2021-01-15 11:06

    This is my code based on Matt Wilko's suggestion:

    Imports System.Net
    Imports System.IO
    Imports System.Text.RegularExpressions
    Public Class Form1
    Dim client As New WebClient
    Public Sub New()
    
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
    
        ' Add any initialization after the InitializeComponent() call.
        AddHandler client.DownloadStringCompleted, AddressOf client_DownloadStringCompleted
        AddHandler client.DownloadProgressChanged, AddressOf client_DownloadProgressChanged
    
    End Sub
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        ProgressBar1.Value = 0
        ProgressBar1.Visible = True
        client.DownloadStringAsync(New Uri("http://somewebsite.com"), Nothing)
    End Sub
    Private Sub client_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs)
        If ProgressBar1.Value < e.ProgressPercentage Then
            ProgressBar1.Value = e.ProgressPercentage
        End If
    End Sub
    Private Sub client_DownloadStringCompleted(ByVal sender As Object, ByVal e As System.Net.DownloadStringCompletedEventArgs)
        ProgressBar1.Value = 100
        Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
        Static waitToCloseProgressBar As Integer
        If ProgressBar1.Value = 100 Then
            If waitToCloseProgressBar > 6 Then
                Timer1.Enabled = False
                waitToCloseProgressBar = 0
                ProgressBar1.Visible = False
            Else
                waitToCloseProgressBar = waitToCloseProgressBar + 1
            End If
        End If
    End Sub
    End Class
    

提交回复
热议问题