使用进度条简单更新标签

时间:2015-05-12 21:01:29

标签: vb.net label progress-bar

我有这个与文件路径一起使用的代码但是我试图让它在完成搜索后将文本更改为“搜索已完成”。这就是我试过的,我尝试了它而没有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

1 个答案:

答案 0 :(得分:0)

There are a few problems here, but the commentators are correct, you are not getting to 100. Additionally, the link here https://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1 will show you how to use a backgroundworker properly. This similar question here will show you how to use the two together: Running a method in BackGroundWorker and Showing ProgressBar