Cursor doesn't hold wait status

164 views Asked by At

while scanning thru the datable I want the cursor to be a wait cursor and showing the progress of the scan

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

The cursor exits the wait because of the Application.DoEvents but when I remove it nothing is showing on the screen.

How can I have a WaitCursor and the DatagridView showing Progress?

As suggested I used the BW. Here is the code:

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

Using the BW painted the lines in DGV but the cursor was in normal state.

0

There are 0 answers