How to stop a timer when mouse is scrolling or on top of scrollbar in listbox

萝らか妹 提交于 2021-02-11 14:20:19

问题


I'm looking for a way to detect and switch off a timer when the mouse cursor is scrolling a listbox. There is an easy way despite to create a new class like this one?link

Would be possible to check rectangle location of listbox 1 scroll bar and say: if mouse is in this range then timer1.stop?

EDIT1: In order to create a rectangle I'm using

 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 are the Top left corner location of the rectangle while the rectangle size is x30 y156 The problem is I don't know in which event let it run! Listbox click event doesn't recognize scrollbar as "inside of listbox" Form_mouse click event doesn't recognize listbox scroll bar as a click in the form. There is an event that despite the control you are on, it will let you play with this workaround? Thanks


回答1:


Here is what I posted on MSDN using this C# code. There is no code presented below that will restart the Timer.

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

Then in your form subscribe to the Scroll event. Requires the ListBox above in your project, one Timer enabled and a 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


来源:https://stackoverflow.com/questions/64137614/how-to-stop-a-timer-when-mouse-is-scrolling-or-on-top-of-scrollbar-in-listbox

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!