计算鼠标滚轮滚动事件发生的次数

Count Number of Times a Mouse Wheel Scroll Event has Occurred

现在,鼠标滚轮滚动更改了我的 DGV 中可见的记录。负 iDelta 移至下一条记录,正 iDelta 移至上一条记录。每条记录包含一个header/footer和dgv中的实际数据。

我正在尝试优化滚动记录的速度。在我开始修改它之前,代码每隔 header/footer 加载一次,然后在每次检测到 MouseWheel 事件时加载记录的整个 dgv。

我正在尝试将其更改为仅加载 header/footer 直到用户暂停(比如约 500 毫秒)的位置。这将确保不会加载不必要的 dgv,从而浪费时间。

我一直在努力寻找一种方法来计算滚轮滚动的次数,一种检测用户是否有一段时间没有滚动鼠标滚轮的方法,或者一种方法在当前鼠标滚轮滚动子例程中调用鼠标滚轮滚动子例程。我认为其中任何一个都会让我走上成功之路。这是我到目前为止所拥有的。显然,它不能按我想要的方式工作。

Private Sub SplitContainer1_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles SplitContainer1.MouseWheel

    Dim iDelta As Integer = e.Delta

    ' Limit the times the stopwatch restarts to every 500ms interval
    If numberOfScrolls = 0 Then
        mouseScrollStopWatch.Start()
    End If

    Select Case iButton
        Case 1, 2
            ' If iDelta is negative move to 'next' record
            If iDelta < 0 Then   

                ' If 500 ms has passed since the 'first' scroll event
                If mouseScrollStopWatch.ElapsedMilliseconds > 500 Then

                    ' Loads the header and footer of the 'next' record  
                    BindPartNumMoveNext() 

                    ' Update the dgv  
                    updateDGV()      

                   ' Reset the stopwatch            
                    mouseScrollStopWatch.Reset()

                   ' Reset number of Scrolls
                    numberOfScrolls = 0                                 

               ' If 500ms has not passed since the 'first' scroll event
                Else

                    ' Loads header and footer                           
                    BindPartNumMoveNext()  

                    ' sets number of scrolls to anything but 0     
                    numberOfScrolls = 1                                 
                End If

            ' If iDelta is positive move to 'previous' record
            Else           

                ' If 500 ms has passed since the 'first' scroll event                         
                If mouseScrollStopWatch.ElapsedMilliseconds > 500 Then 

                    ' Loads the header and footer of the 'previous' record
                    BindPartNumMovePrevious()    

                    ' Update the dgv
                    updateDGV()                  

                    ' Reset the stopwatch
                    mouseScrollStopWatch.Reset()                        

                    ' Reset number of Scrolls
                    numberOfScrolls = 0    

                ' If 500ms has not passed since the 'first' scroll event
                Else                          

                    ' Loads header and footer  
                    BindPartNumMovePrevious()           

                    ' sets number of scrolls to anything but 0
                    numberOfScrolls = 1                                 
                End If
            End If

        ' These cases can be ignored for now
        Case 3, 4                                                       
            If iDelta < 0 Then
                BindRefDesMoveNext()
            Else
                BindRefDesMovePrevious()
            End If
    End Select

End Sub

在我尝试的这个版本中,问题是如果 500 毫秒没有传递到特定的鼠标滚轮滚动,则 dgv 永远不会加载(即,如果最后一个 wheelScroll 事件发生在 mouseScrollStopWatch.ElapsedMilliseconds < 500 时,则 dgv永远不会加载。)

关于如何从鼠标滚轮捕获更多数据的任何建议?最终目标只是提高我浏览记录的速度,因此我对不围绕我在此处概述的那些解决方案的其他解决方案持开放态度。

我已尝试具体说明,但我可能会接近它以意识到我没有包含必要的信息。如果我不是,请告诉我,我会把它包括在内。

我认为您需要做的就是使用定时器来确保在滚动停止 500 毫秒时调用 updateDGV()。我只是使用一个文本框作为滚动的主题,另一个文本框来指示正在发生的事情:

Public Class Form1

    Dim scrollTimer As Timer
    Const SCROLLWAIT As Integer = 500

    Dim iButton As Integer = 1

    Private Sub SetupScrollTimer()
        scrollTimer = New Timer
        scrollTimer.Interval = SCROLLWAIT
        AddHandler scrollTimer.Tick, AddressOf FinalScroll
    End Sub

    Private Sub FinalScroll(sender As Object, e As EventArgs)
        TextBox1.AppendText("updateDGV()" & vbCrLf)
        scrollTimer.Stop()
    End Sub

    Private Sub TextBox2_MouseWheel(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox2.MouseWheel
        scrollTimer.Start()
        Dim iDelta As Integer = e.Delta

        If iDelta < 0 Then
            TextBox1.AppendText("BindPartNumMoveNext()" & vbCrLf)
        Else
            TextBox1.AppendText("BindPartNumMovePrevious()" & vbCrLf)
        End If

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SetupScrollTimer()

    End Sub

End Class

Windows.Forms.Timer.Start() 旨在将计时器重置为零,但仍然存在对 updateDGV() 的虚假调用。如果这些是问题,那么您将需要调整代码(问题留作 reader 的练习)。