0

通过数据表扫描时,我希望光标成为等待光标并显示扫描进度

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Cursor.Current = Cursors.WaitCursor
    For k = 0 To vCountries.Count - 1
        If k < DgView1.FirstDisplayedScrollingRowIndex OrElse k > DgView1.FirstDisplayedScrollingRowIndex +
                                                                  DgView1.DisplayedColumnCount(False) Then
            DgView1.FirstDisplayedScrollingRowIndex = k
        End If
        DgView1.Rows(k).Cells(CoName).Style.BackColor = Color.GreenYellow
        Application.DoEvents()
        ScannerPageOne(vCountries.Item(k).Row)
        DgView1.Rows(k).Cells(CoName).Style.BackColor = DgView1.DefaultCellStyle.BackColor
    Next
    Cursor.Current = Cursors.Default
End Sub

由于 Application.DoEvents,光标退出等待,但是当我将其删除时,屏幕上没有显示任何内容。

如何让 WaitCursor 和 DatagridView 显示进度?

正如建议的那样,我使用了 BW。这是代码:

WithEvents BW As New BackgroundWorker With {.WorkerReportsProgress = True}
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Cursor.Current = Cursors.WaitCursor
        BW.RunWorkerAsync(vCountries)
    End Sub
    Private Sub BW_DoWork(sender As Object, e As DoWorkEventArgs) Handles BW.DoWork
        Dim bw As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim vCountries As DataView = e.Argument
        For k = 0 To vCountries.Count - 1
            bw.ReportProgress(k)
            ScannerPageOne(vCountries.Item(k).Row)
        Next
    End Sub
    Private Sub BW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) _
        Handles BW.RunWorkerCompleted
        Cursor.Current = DefaultCursor
    End Sub
    Private Sub BW_ProgressChanged(ByVal sender As System.Object, ByVal e As ProgressChangedEventArgs) _
        Handles BW.ProgressChanged
        If e.ProgressPercentage > 0 Then
            DgView1.Rows(e.ProgressPercentage - 1).Cells(CoName).Style.BackColor = DgView1.DefaultCellStyle.BackColor
        End If
        DgView1.Rows(e.ProgressPercentage).Cells(CoName).Style.BackColor = Color.GreenYellow
    End Sub

使用 BW 在 DGV 中绘制线条,但光标处于正常状态。

4

0 回答 0