【问题标题】:VB reset progress bar value after downloadVB下载后重置进度条值
【发布时间】:2013-05-05 15:17:57
【问题描述】:

作为一种练习,我试图让我的程序通过进度条下载文件。它下载它很好并且进度条跟随但问题是下载完成后它不会重置。这是按钮和栏的代码。

Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    WC.DownloadFileAsync(New Uri("imageurlhere"), "c:\myfile.jpg")
    If ProgressBar1.Value = ProgressBar1.Maximum Then
        ProgressBar1.Value = ProgressBar1.Minimum
    End If
End Sub

Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
    ProgressBar1.Value = e.ProgressPercentage
End Sub

【问题讨论】:

    标签: vb.net image download progress


    【解决方案1】:

    试试这个

    Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        WC.DownloadFileAsync(New Uri("imageurlhere"), "c:\myfile.jpg")
    End Sub
    
    Private Sub WC_DownloadProgressChanged(ByVal sender As Object, ByVal e As DownloadProgressChangedEventArgs) Handles WC.DownloadProgressChanged
        ProgressBar1.Value = e.ProgressPercentage
        If ProgressBar1.Value = ProgressBar1.Maximum Then
            ProgressBar1.Value = ProgressBar1.Minimum
        End If
    End Sub
    

    【讨论】:

    • 没用,之前试过(改了reset函数的地方)
    • 设置 ProgressBar1.Maximum = 1(100% 值)
    • 由于某种原因,它现在可以工作了... ish,if 函数确实重置了条形图,但问题是绿色条形在 if 函数重置它之前无法到达末尾,但是我认为可以通过缩短栏来解决。
    • 我不这么认为..我可以知道你的 ProgressBar1.Minimum 值吗?
    • 它没有在任何地方设置。价值函数只通过我猜是未定义的东西的百分比来移动它。
    【解决方案2】:

    进度条活动可能非常不确定,在取消 WebClient.CancelAsync() 运行此客户端的函数时的最终行为中的预期操作和功能(如您认为它应该表现的那样)分配到应用程序内。我不知道下载完成后进度条会重置为最小值而没有被取消。所以,我将提供两个我用来玩的sn-ps代码,作为似乎是Windows窗体开发/ ASP.NET开发编程的“简单”起点的设计基础,'BAREBONES'版本,不使用处理下载活动更改的后台工作人员实例。

    1. 似乎“取消”正在进行的当前下载似乎引发了一些奇怪的行为,因为单击按钮似乎并没有正确引发取消事件以导致“停止”和“重置”中断取消按钮的按钮单击表单,与按钮单击事件中的 CancelAsync() 一起使用。因此,我看到了几种不同的行为,具体取决于分配给此事件代码逻辑中实现的进度条属性/方法的内容。

    因此,我似乎只有一个简单的解决方法,即使用 MessageBox.Show() 中断或与消息框表单具有相同目的的自定义表单。

    Private Sub Button1_Click(sender As Object, e As EventArgs) 处理 Button1.Click

        ' THE Kill() IS NOT NECESSARY IF THE MessageBox Show() is IMPLEMENTED AND USED
        'Kill(TextBox2.Text) ' since the Cancel button does not fully stop the download
        ' and clear the prgress bar to reset that to "0", this is used to stop the download
        ' at the root path of the saved file destinatio !!!!n
        download.CancelAsync()
        ProgressBar1.Disposing.ToString()
        ' ProgressBar1.Dispose() ' hides the progress bar here
        ProgressBar1.Update()
        ProgressBar1.Value = 100
        ProgressBar1.Show()
        LastCount = 0
    
        ' Label4.ResetText()
        Label4.BackColor = Color.LightYellow
        Label4.Text = "THE FILE DOWNLOAD WAS CANCELLED .. A - N - D .. ALSO ABORTED !!!!!!!"
        Label4.Visible = True
        ' Label5.ResetText()
        Label5.Text = "NO TRANSFER RATE - FILE DOWNLOAD CANCEL and ABORTED"
        Label5.Visible = True
        'Label6.ResetText()
        Label6.Text = "Saved Copy of File Download is : "
        'Label7.ResetText()
        Label7.Text = "Download Source File Size is : "
        Label8.ResetText()
        Label8.Text = ""
        'Label10.ResetText()
        'Label10.Text += Label10.Text & " was the previous file downloading transfer rate...."
    
    
        ' trows error if there is not a file in the directory
        ' IO.File.Delete(savedLocation) ' delete the file in the chosen directory, if present
    
        cmdsave.Enabled = True
        Button1.Enabled = False
        Button2.Enabled = False
        Button3.Enabled = False
        'Label4.Visible = True ' to alleviate the text that will be shown as the "old"
        'Label5.Visible = True ' values of the cancelled download, so make these not visible
        ' ...... make the labels "invislbe" now, but make them "visible" when the
        ' ...... cmdsae button is clicked, or see if the form load can make them
        ' ...... visbile again  -->>> POST THE CANCELLATION MESSAGE IN
        ' .....  TEXTBOX1 OR LABEL
    
    
    
        If ProgressBar1.Value = 100 Then
    
            Label4.ResetText()
            Label4.BackColor = Color.Transparent
            Label4.Text = ""
            ' Button1.Enabled = False ' DOES NOT WORK HERE !!
            MessageBox.Show("FILE DOWNLOAD WAS CANCELLED / ABORTED !!")
    
            Button1.Enabled = False ' DOES W-O-R-K -- GREAT -- HERE !!
            ProgressBar1.Value = 0
            ProgressBar1.Update()
            ' ProgressBar1.Hide()
            ' ProgressBar1.Visible = False
            'Label4.ResetText()
            Label4.BackColor = Color.Yellow
            Label4.Text = "THE FILE DOWNLOAD WAS CANCELLED ABD ABORTED!!!!!"
            Label4.Visible = True
            ' Label5.ResetText()
            Label5.Text = "NO TRANSFER RATE - FILE DOWNLOAD CANCEL and ABORTED"
            Label5.Visible = True
            'Label6.ResetText()
            Label6.Text = "Saved Copy of File Download is : "
            'Label7.ResetText()
            Label7.BackColor = Color.Transparent
            Label7.Text = "Download Source File Size is : "
            Label8.ResetText()
            Label8.Text = ""
            'Label10.ResetText()
            Label10.Text = Label10.Text & " was the previous file downloading transfer rate...."
    
            Return
        End If
    
        download.Dispose()
    
        MessageBox.Show("")
    
        Return
    
        Kill(TextBox1.Text)
    
    End Sub
    
    1. 如果使用 BackgroundWokrer,那么它也会很有用,但是 BackgroundWorker 实例可以处理各种方式来执行此操作,例如使用可以处理表单下载活动重定向的类,但这需要更多代码显示活动/事件过程的粒度,从而使数字“1”。选项作为手头的最佳解决方案,可能不是完全用于代码模块化/模块化,而是用于代码活动区分而不编写更多代码并将其包装为测试中可能的故障点,这是我的选择/选项实现!!

    【讨论】:

      【解决方案3】:

      进度条活动可能非常不确定,在取消 WebClient.CancelAsync() 运行此客户端的函数时的最终行为中的预期操作和功能(如您认为它应该表现的那样)分配到应用程序内。我不知道下载完成后进度条会重置为最小值而没有被取消。所以,我将提供两个我用来玩的sn-ps代码,作为似乎是Windows窗体开发/ ASP.NET开发编程的“简单”起点的设计基础,'BAREBONES'版本,不使用处理下载活动更改的后台工作人员实例。

      1. 似乎“取消”正在进行的当前下载似乎引发了一些奇怪的行为,因为单击按钮似乎并没有正确引发取消事件以导致“停止”和“重置”中断取消按钮的按钮单击表单,与按钮单击事件中的 CancelAsync() 一起使用。因此,我看到了几种不同的行为,具体取决于分配给此事件代码逻辑中实现的进度条属性/方法的内容。

      因此,我似乎只有一个简单的解决方法,即使用 MessageBox.Show() 中断或与消息框表单具有相同目的的自定义表单。

      Private Sub Button1_Click(sender As Object, e As EventArgs) 处理 Button1.Click

          ' THE Kill() IS NOT NECESSARY IF THE MessageBox Show() is IMPLEMENTED AND USED
          'Kill(TextBox2.Text) ' since the Cancel button does not fully stop the download
          ' and clear the prgress bar to reset that to "0", this is used to stop the download
          ' at the root path of the saved file destinatio !!!!n
          download.CancelAsync()
          ProgressBar1.Disposing.ToString()
          ' ProgressBar1.Dispose() ' hides the progress bar here
          ProgressBar1.Update()
          ProgressBar1.Value = 100
          ProgressBar1.Show()
          LastCount = 0
      
          ' Label4.ResetText()
          Label4.BackColor = Color.LightYellow
          Label4.Text = "THE FILE DOWNLOAD WAS CANCELLED .. A - N - D .. ALSO ABORTED !!!!!!!"
          Label4.Visible = True
          ' Label5.ResetText()
          Label5.Text = "NO TRANSFER RATE - FILE DOWNLOAD CANCEL and ABORTED"
          Label5.Visible = True
          'Label6.ResetText()
          Label6.Text = "Saved Copy of File Download is : "
          'Label7.ResetText()
          Label7.Text = "Download Source File Size is : "
          Label8.ResetText()
          Label8.Text = ""
          'Label10.ResetText()
          'Label10.Text += Label10.Text & " was the previous file downloading transfer rate...."
      
      
          ' trows error if there is not a file in the directory
          ' IO.File.Delete(savedLocation) ' delete the file in the chosen directory, if present
      
          cmdsave.Enabled = True
          Button1.Enabled = False
          Button2.Enabled = False
          Button3.Enabled = False
          'Label4.Visible = True ' to alleviate the text that will be shown as the "old"
          'Label5.Visible = True ' values of the cancelled download, so make these not visible
          ' ...... make the labels "invislbe" now, but make them "visible" when the
          ' ...... cmdsae button is clicked, or see if the form load can make them
          ' ...... visbile again  -->>> POST THE CANCELLATION MESSAGE IN
          ' .....  TEXTBOX1 OR LABEL
      
      
      
          If ProgressBar1.Value = 100 Then
      
              Label4.ResetText()
              Label4.BackColor = Color.Transparent
              Label4.Text = ""
              ' Button1.Enabled = False ' DOES NOT WORK HERE !!
              MessageBox.Show("FILE DOWNLOAD WAS CANCELLED / ABORTED !!")
      
              Button1.Enabled = False ' DOES W-O-R-K -- GREAT -- HERE !!
              ProgressBar1.Value = 0
              ProgressBar1.Update()
              ' ProgressBar1.Hide()
              ' ProgressBar1.Visible = False
              'Label4.ResetText()
              Label4.BackColor = Color.Yellow
              Label4.Text = "THE FILE DOWNLOAD WAS CANCELLED ABD ABORTED!!!!!"
              Label4.Visible = True
              ' Label5.ResetText()
              Label5.Text = "NO TRANSFER RATE - FILE DOWNLOAD CANCEL and ABORTED"
              Label5.Visible = True
              'Label6.ResetText()
              Label6.Text = "Saved Copy of File Download is : "
              'Label7.ResetText()
              Label7.BackColor = Color.Transparent
              Label7.Text = "Download Source File Size is : "
              Label8.ResetText()
              Label8.Text = ""
              'Label10.ResetText()
              Label10.Text = Label10.Text & " was the previous file downloading transfer rate...."
      
              Return
          End If
      
          download.Dispose()
      
          MessageBox.Show("")
      
          Return
      
          Kill(TextBox1.Text)
      
      End Sub
      
      1. 如果使用 BackgroundWokrer,那么它也会很有用,但是 BackgroundWorker 实例可以处理各种方式来执行此操作,例如使用可以处理表单下载活动重定向的类,但这需要更多代码显示活动/事件过程的粒度,从而使数字“1”。选项作为手头的最佳解决方案,可能不是完全用于代码模块化/模块化,而是用于代码活动区分而不编写更多代码并将其包装为测试中可能的故障点,这是我的选择/选项实现!!

      值得一提的是,与下载后更改和“归零”进度条有关的典型进度更改过程具有如上所述的正常语法/语义表达,即 matzone 在 THIS POST 中给出的答案,并且可能由另一位专家或知识渊博的 .NET 程序员对此问题给出的答案。我个人处理基本进度条“重置”,在成功并完成下载过程后下载文件,并在取消当前、活动下载(进行中)时,使用 WEBCLIENT CONTROL 的 DownloadFileAsync 作为:

      私人子......

          ProgressBar1.Value = e.ProgressPercentage
      
      
      If e.ProgressPercentage > LastCount Then
      
          If ProgressBar1.Value = ProgressBar1.Maximum Then
      
              Label4.ResetText()
              Label4.BackColor = Color.AntiqueWhite
              Label4.Text = "File Download Is Complete"
              Label7.ResetText()
              Label7.BackColor = Color.Gold
              Label7.Text = "DOWNLOAD STATUS : File was successfully downlaoded !!"
      
              Label8.ResetText()
              Label8.Text = "LAST DOWNLOADED FILE : " + TextBox2.Text.ToString +
          " with a size of " & GetDownloadSize(TextBox1.Text) & " KB , " +
          "was downloaded on" + DateTime.Now + " from the URL / URI at " +
          TextBox1.Text.ToString + " with a download transfer rate of " +
          "approximately, ( ~ ) " + (e.BytesReceived / (DirectCast(e.UserState, Stopwatch).ElapsedMilliseconds / 1000.0#)).ToString("#") & " KB. "
      

      ' 做其他表单功能/特性,如果需要,例如重置表单的按钮,如下所示:

              cmdsave.Enabled = True
              Button1.Enabled = False
              Button2.Enabled = False
              Button3.Enabled = True
              viewDLfile.Enabled = True
              cmdPaste.Enabled = False
              ProgressBar1.Value = Nothing
              ' TextBox1.Clear() ; seems to throw a 
      
              Button1.Focus()
      
              LastCount = 0
      
              Return
          End If
      
      End If
      

      结束子

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多