如何在鼠标滚动或列表框中滚动条顶部时停止计时器

问题描述

我正在寻找一种在鼠标光标滚动列表框时检测并关闭计时器的方法。 尽管创建了像这样的新类,但是有一种简单的方法吗?link

是否可以检查列表框1滚动条的矩形位置并说:如果鼠标在此范围内,则timer1.stop?

EDIT1: 为了创建一个矩形,我正在使用

 If e.X >= 364 AndAlso e.X <= 446 AndAlso e.Y >= 86 AndAlso e.Y <= 144 Then
        MessageBox.Show("Clicked within the rectangle")
    Else
        MessageBox.Show("Clicked outside the rectangle")
    End If

449-359是矩形的左上角位置 而矩形尺寸为x30 y156 问题是我不知道在哪个事件中让它运行! 列表框单击事件无法将滚动条识别为“列表框内部” Form_mouse click事件无法将列表框滚动条识别为表单中的单击。 发生一个事件,尽管您处于控制状态,但可以使用此替代方法吗? 谢谢

解决方法

这是我使用on MSDN发布的this C# code。下面没有代码会重启计时器。

Public Class BetterListBox
    Inherits ListBox

    ' Event declaration
    Public Delegate Sub BetterListBoxScrollDelegate(ByVal Sender As Object,ByVal e As BetterListBoxScrollArgs)
    Public Event Scroll As BetterListBoxScrollDelegate
    ' WM_VSCROLL message constants
    Private Const WM_VSCROLL As Integer = &H115
    Private Const SB_THUMBTRACK As Integer = 5
    Private Const SB_ENDSCROLL As Integer = 8

    Protected Overrides Sub WndProc(ByRef m As Message)
        ' Trap the WM_VSCROLL message to generate the Scroll event
        MyBase.WndProc(m)

        If m.Msg = WM_VSCROLL Then
            Dim nfy As Integer = m.WParam.ToInt32() And &HFFFF
            If (nfy = SB_THUMBTRACK OrElse nfy = SB_ENDSCROLL) Then
                RaiseEvent Scroll(Me,New BetterListBoxScrollArgs(Me.TopIndex,nfy = SB_THUMBTRACK))
            End If
        End If
    End Sub
    Public Class BetterListBoxScrollArgs
        ' Scroll event argument
        Private mTop As Integer
        Private mTracking As Boolean
        Public Sub New(ByVal top As Integer,ByVal tracking As Boolean)
            mTop = top
            mTracking = tracking
        End Sub
        Public ReadOnly Property Top() As Integer
            Get
                Return mTop
            End Get
        End Property
        Public ReadOnly Property Tracking() As Boolean
            Get
                Return mTracking
            End Get
        End Property
    End Class
End Class

然后在您的表单中订阅Scroll事件。在您的项目中需要上面的ListBox,启用一个Timer和一个Label。

Private Sub BetterListBox1_Scroll(Sender As Object,e As BetterListBox.BetterListBoxScrollArgs) _
    Handles BetterListBox1.Scroll

    Timer1.Enabled = False

End Sub

Private Sub Timer1_Tick(sender As Object,e As EventArgs) Handles Timer1.Tick
    Label1.Text = Now.ToString()
End Sub