We help IT Professionals succeed at work.

Datagridview crashes when scrolling if mouse button is pressed.

rowternet
rowternet used Ask the Experts™
on
Hi,

I have a datagridview and a timer on it.
This timer refreshes every 12 seconds.
Whenever i use the horizontal scroll if the mouse button is pressed ,the datagridview crashes and i have to restart teh application.

This is the code i have in the timer tick event.
 Private Sub Sh_Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Sh_Timer1.Tick
        Do While Not bFormClose
            If MouseButtons = Windows.Forms.MouseButtons.None Then #### if mouse is not pressed
                If Not bRowBeingEdited Then #### if row is not being edited
                    refreshDGVMethod() #### refresh the datagrid.
                    Exit Sub
                ElseIf MouseButtons = Windows.Forms.MouseButtons.Left Then
                    Exit Sub
                End If
            End If

        Loop
    End Sub

Not sure why i am getting the problem.I do not have anything written in the scroll event.

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
If you place a SH_Timer1.Stop() just before the call to edit the row, then start it up again when the editing is done, I think you will be ok. Also, if the timer stops when the form closes, what is the point of the do While? That will cause the timer.tick event to run continuously after the first tick until there is left click.

Author

Commented:
Hi tommyboy,

I do nto have a problem with editin the row. I am having this issue only if i am scrolling(mouse button down) and the datagridview refreshes when scrolling.
I also aded this code in the mouse button down event.
I am stopping the timer if the mouse is down(pressed) and starting if the muse is released. But i still see the crash.

  Private Sub dgvSchedules_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvSchedules.MouseDown
        If MouseButtons.Left Then
            Sh_Timer1.Stop()
        ElseIf MouseButtons.None Then
            Sh_Timer1.Start()
        End If
    End Sub

Thanks
Top Expert 2015
Commented:
That Do While is still suspect to me. Have you tried commenting it out?
Top Expert 2015

Commented:
Also, I don't think you are actually capturing the mousedown. Shouldn't be:
If e.Button = MouseButtons.Left Then
            SH_Timer1.Stop()
        ElseIf e.Button = MouseButtons.None Then
            SH_Timer1.Start()
        End If

Author

Commented:
Hi tommyboy , i think the problem was with the do while loop.
I removed it and  i am not getting error.
I will leave this open for couple days andthen close this question.
Thanks

Author

Commented:
Thanks