【问题标题】:Simple updating label with progress bar带有进度条的简单更新标签
【发布时间】:2015-05-12 21:01:28
【问题描述】:

我有这个与文件路径一起工作的代码,但我试图让它在完成搜索后将文本更改为“搜索完成”。这是我尝试过的,我在没有 100 左右的引号的情况下尝试了它,但它仍然无法正常工作,任何指针。

Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim argument = DirectCast(e.Argument, Tuple(Of String, String))
    Dim target = argument.Item1
    Dim folderPath = argument.Item2
    Dim filePaths = IO.Directory.GetFiles(folderPath, "*.txt")

    'Report the total file count.
    Me.BackgroundWorker1.ReportProgress(filePaths.Length)

    For Each filePath In filePaths
        'Report progress.
        Me.BackgroundWorker1.ReportProgress(CInt(False), filePath)

        If IO.File.ReadAllText(filePath).Contains(target) Then
            'Report a successful search.
            Me.BackgroundWorker1.ReportProgress(CInt(True), filePath)
        End If
    Next

End Sub
Private Sub backgroundworker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
    Dim filePath = DirectCast(e.UserState, String)

    If filePath Is Nothing Then
        'This is the total file count.
        Me.dataprogbar.Maximum = e.ProgressPercentage
    ElseIf CBool(e.ProgressPercentage) Then
        'This is a successful search.
        Me.ListBox1.Items.Add(filePath)
    Else
        'This is a simple progress update.
        Me.dataprogbar.PerformStep()
        Me.Label1.Text = filePath
        If dataprogbar.Value = "100" Then
            Label1.Text = "Search Completed"
        End If
    End If
End Sub

【问题讨论】:

  • 那么当您“完成搜索”时,dataprogbar.Value 的值是多少?我猜它不是 100。
  • @TheBlueDog 最大值设置为100
  • @TheBlueDog 我添加了整个代码,所以最大值 = e.progresspercentage
  • 我再问一次,当你“搜索完毕”时,dataprogbar.Value 的值是多少?此时,您将最大值设置为什么都无关紧要。
  • "the value should be 100 when done searching" 它应该是,或者它是 = 100 ?

标签: vb.net label progress-bar


【解决方案1】:

这里有一些问题,但评论员是正确的,你没有达到 100。此外,https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 的链接将向你展示如何正确使用后台工作程序。这个类似的问题将向您展示如何将两者结合使用:Running a method in BackGroundWorker and Showing ProgressBar

【讨论】:

  • Progressbar 在完成之前就已经达到最大值,并且仍然不更改标签:/
  • 好的,所以它说搜索在它完成之前完成并且即使它说它已经完成也继续搜索。
  • 现在它正在添加所有文本文件而不是搜索字符串所在的位置,并且在搜索完成后仍然继续搜索,感谢您的所有帮助。
  • Np,您的进度条不会填满,除非您“报告进度”的次数与您设置的最大值一样多。因此,如果您继续添加 所有文件 的计数并且只更新 匹配 您永远不会填写向上进度条。
  • 另外,除非有其他人突然闯入,否则我回家后会回答更多问题。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多