下载后VB重置进度条值

时间:2013-05-05 15:17:57

标签: vb.net image download progress

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

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

3 个答案:

答案 0 :(得分:0)

试试这个

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

答案 1 :(得分:0)

在取消 WebClient.CancelAsync() 运行此客户端的功能时的最终行为中,进度条活动可能非常不确定,在操作和功能方面(正如您认为它应该表现的那样)分配给应用程序内。我不知道在下载完成后将进度条重置为最小值而不被取消。因此,我将提供两个我用来玩的代码片段,作为设计基础,似乎是 Windows 窗体开发/ASP.NET 开发编程的“简单”起点,“准系统”版本,无需使用背景用于处理下载活动更改的 Worker 实例。

  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”。选项作为手头的最佳解决方案,可能不太适合代码模块化/模块化,但对于代码活动的区别,无需编写更多代码并将其包装为测试中可能的失败点,这是我的选择/选项实现!!

答案 2 :(得分:0)

在取消 WebClient.CancelAsync() 运行此客户端的功能时的最终行为中,进度条活动可能非常不确定,在操作和功能方面(正如您认为它应该表现的那样)分配给应用程序内。我不知道在下载完成后将进度条重置为最小值而不被取消。因此,我将提供两个我用来玩的代码片段,作为设计基础,似乎是 Windows 窗体开发/ASP.NET 开发编程的“简单”起点,“准系统”版本,无需使用背景用于处理下载活动更改的 Worker 实例。

  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 在此帖子中给出的答案,并且可能会由另一位专家或知识渊博的 .NET 程序员就此问题给出答案。我个人处理 BASIC 进度条“重置”,在下载文件成功并完成下载程序后,并在取消当前的主动下载(进行中)时,使用 WEBCLIENT 控件的 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

结束子