在通过数据存储进行扫描时,我希望光标成为等待光标并显示扫描进度
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中绘制线条,但光标处于正常状态。