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

时间:2015-05-08 15:51:21

标签: vb.net winforms scroll vb.net-2010 mousewheel

现在,MouseWheel滚动会更改我的DGV中可查看的记录。负iDelta移动到下一个记录,正iDelta移动到前一个记录。每条记录都包含页眉/页脚和dgv中的实际数据。

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

我试图将其更改为只加载页眉/页脚的位置,直到用户暂停(比如说〜500ms)。这样可以确保不会加载不必要的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

在我的尝试版本中,问题是dgv永远不会被加载,如果500ms没有传递特定的鼠标滚轮滚动(即如果最后的wheelScroll事件发生在mouseScrollStopWatch.ElapsedMilliseconds&lt; 500然后dgv从不加载。)

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

我试图具体,但我可能会接近它,以便意识到我没有包含重要信息。如果我没有,请告诉我,我会把它包括在内。

1 个答案:

答案 0 :(得分:1)

我认为您需要做的就是使用Timer来确保在滚动停止500 ms时调用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()的虚假调用。如果这些是一个问题,那么你将需要调整代码(问题留给读者练习)。