作为一种练习,我试图通过进度条让我的程序下载文件。它下载很好,进度条如下,但问题是下载完成后它不会重置。这是按钮和条形码的代码。
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
答案 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 实例。
因此,我似乎只有一个简单的解决方法,即使用 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
答案 2 :(得分:0)
在取消 WebClient.CancelAsync() 运行此客户端的功能时的最终行为中,进度条活动可能非常不确定,在操作和功能方面(正如您认为它应该表现的那样)分配给应用程序内。我不知道在下载完成后将进度条重置为最小值而不被取消。因此,我将提供两个我用来玩的代码片段,作为设计基础,似乎是 Windows 窗体开发/ASP.NET 开发编程的“简单”起点,“准系统”版本,无需使用背景用于处理下载活动更改的 Worker 实例。
因此,我似乎只有一个简单的解决方法,即使用 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
就其价值而言,与下载后更改和“归零”进度条相关的典型进度更改过程具有如上所述的正常语法/语义表达式,即 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
结束子