vb.net 状态栏文本不显示

vb.net statusbar text doesn't show

我有一个代码可以在文档中搜索单词并用找到的文档填充 Listview。因为这个过程可能会很长,所以我在状态栏中发出了警告。

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

    Dim foundList As Boolean
    Dim docImage As VariantType
    Dim PresetName As String
    ListView1.Items.Clear()
    ToolStripLabel1.Text = "Searching your documents, please wait."
    Try
        For Each row As DataRowView In CheckedListBox1.CheckedItems
            SearchRegX = row("RegX")
        Next
        If TextBoxFreeText.Text <> "" Then
            'look for a space in the search criteria, meaning there are more words
            Dim counter As Integer = TextBoxFreeText.Text.IndexOf(" ")
            If counter <> -1 Then
                'more words were entered to search
                Dim varSplit As Object
                varSplit = Split(TextBoxFreeText.Text, " ")
                'if more than two words are entered our regex doesnt work, so we exit the sub
                If varSplit.length > 2 Then
                    MsgBox("Your search criteria are to complex, use a maximum of two words",, Title)
                    Exit Sub
                End If

                iWords = NumericUpDown1.Value
                SearchRegX = "(?i)\b(?:" + varSplit(0) + "\W+(?:\w+\W+){0," + iWords + "}?" + varSplit(1) + "|" + varSplit(1) + "\W+(?:\w+\W+){0," + iWords + "}?" + varSplit(0) + ")\b"
            Else
                'just one word was entered
                SearchRegX = "(?i)\b" + TextBoxFreeText.Text + "\b"
            End If
        End If
            If SearchRegX = "" Then
            MsgBox("No Keyword was selected",, Title)
            Exit Sub
        End If

        If ListBox1.SelectedIndex > -1 Then
            For Each Item As Object In ListBox1.SelectedItems

                Dim ItemSelected = CType(Item("Path"), String)
                SearchFolder = ItemSelected

                'check if the folder of the archive still exists
                If (Not System.IO.Directory.Exists(SearchFolder)) Then
                    Dim unused = MsgBox("The archive " + SearchFolder.Substring(SearchFolder.Length - 5, Length) + " was not found",, Title)
                    Continue For
                End If
                Dim dirInfo As New IO.DirectoryInfo(SearchFolder)
                Dim files As IO.FileInfo() = dirInfo.GetFiles()
                Dim file As IO.FileInfo

                docImage = ImageList1.Images.Count - 1
                Dim items As New List(Of ListViewItem)

                For Each file In files
                    Dim filename As String = file.Name.ToString

                    If file.Extension = ".pdf" Or file.Extension = ".PDF" Then
                        foundList = PDFManipulation.GetTextFromPDF2(SearchFolder + filename, SearchRegX)
                        If foundList = True Then
                            If ListView1.FindItemWithText(filename.ToString) Is Nothing Then
                                items.Add(New ListViewItem(New String() {"", filename.ToString, SearchFolder.ToString}, docImage))
                            End If
                        End If
                    End If
                Next
                ListView1.Items.AddRange(items.ToArray)
            Next
            ToolStripLabel1.Text = ListView1.Items.Count.ToString + " Documents found."
        Else
            MsgBox("No archive was selected",, Title)
        End If

        SearchRegX = ""
        SearchFolder = ""

        'now save the search word to our textfile
        PresetName = TextBoxFreeText.Text
        If PresetName <> "" Then
            AddSearchWord()
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

不知何故,在进程开始之前文本没有显示在状态栏中。

我应该改变什么? 我考虑过 BackgroundWorker 显示像等待这样的图像,但 BackgroundWorker 不起作用,因为在我的 Sub 中我在别处调用了一个函数。

发生这种情况是因为在挂起的工作负载完成之前状态条控件未正确呈现。为此,您可以手动刷新状态条:

StatusStrip1.Refresh()

也就是说,如果您的唯一目标是设置文本。如果您考虑 运行 在后台设置代码以便表单仍然响应用户输入,您将需要使用 System.Threading.TasksSystem.Threading.Thread 到 运行 的异步编程代码作为一个单独的线程。请注意,在尝试访问主线程之外的控件时可能会遇到困难。

Async 添加到您的按钮处理程序声明中:

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

现在更改对 PDFManipulation.GetTextFromPDF2() 的调用,使其在任务中:

Await Task.Run(Sub()
                   foundList = PDFManipulation.GetTextFromPDF2(SearchFolder + filename, SearchRegX)
               End Sub)

If foundList = True Then
    ...