locked
Stop a timer when user is scrolling/dragging listbox scrollbar RRS feed

  • Question

  • Hi, How could I stop a timer when :

    -User is scrolling listbox scrollbar

    -user cursors  is on top of listbox scrollbar 

    Thanks

    Wednesday, September 30, 2020 2:54 PM

Answers

  • The following addresses what you asked for but doesn't handle starting the timer again.

    Original 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
    

    Simple example where at this point the Timer is enabled.

    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


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Mattia Fanti Wednesday, September 30, 2020 6:43 PM
    Wednesday, September 30, 2020 4:49 PM

All replies

  • The following addresses what you asked for but doesn't handle starting the timer again.

    Original 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
    

    Simple example where at this point the Timer is enabled.

    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


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Mattia Fanti Wednesday, September 30, 2020 6:43 PM
    Wednesday, September 30, 2020 4:49 PM
  • Hi Karen,

    I am having issues with this code. The class is working properly if I build the project in 32 bit while my computer is a 64bit. I'm trying to built it using any cpu but it doesn't let my open the form designer until i build it again in 32 bit. I am also noticing that after a while, the control is disappearing without any reason.. I've been searching in the beginning cause VS was failing to load the control on toolbox. 

    I found this topic and it seems a common problem link

    so basically the control is not working If I am building it with " any cpu" or "x64". Both release or debug doesn't change or solve the issue. 

    Any idea? 

    Thanks

    Wednesday, September 30, 2020 10:10 PM
  • Unfortunately you need to settle on one configuration and when changing as you learned sometimes what has happened here is the norm, no getting around this.

    Custom components are easy while custom controls can be challenging when flip-flopping CPU configurations.

     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, September 30, 2020 10:49 PM
  • Oh I see..  unfortunately on my x64 laptop building the project on x32 means my.settings not working at all, it doesnt save or show at all, plus the form dimension  is getting bigger...  Seriously there is not workaraund to let it work on any cpu?

    Thanks

    Thursday, October 1, 2020 1:06 AM
  • In a standard project My.Settings works on x86 and x64, if not working for you on either one there is something odd going on.

    What I do is use appSettings in app.config with a class that can read and update at runtime e.g.

    Partial, actual app this came from has a handful more settings.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
        </startup>
      <appSettings>
        <add key="StagingFolder" value = "D:\UISides\oed_staging_incoming" />
        <add key="IncomingFolder" value = "D:\UISides\oed_incoming" />
        <add key="TestMode" value = "true" />
        <add key="importMinutesToPause" value="1" />
        <add key="LinesToSplit" value="1000" />
        <add key="LinesToSplitByBroker" value="1000" />
      </appSettings>
    </configuration>

    Sure it would be nice if this was not needed but after 20 years writing apps in .NET I found this works for me. The code I use is C#, if interested sometime tomorrow I will see about converting it to VB.NET.

    Form getting bugger usually only happens when changing screen resolution and the IDE messes with your code.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 1, 2020 1:58 AM
  • Hi Karen, I will try again in a empty project and let s see if it working or not.

    I assume that code is the replacement of my.settings right? So are you advising me to use app setting? I d like to see your vb.net code if possible.

    Did you try to replicate my error adding betterlistbox class, use a normal my.setting and build it on debug x64?

    Thanks

    Edit1. Is the project running at x64 working for you also without my.settings or app settings? I mean just using the class ?


    Thursday, October 1, 2020 12:15 PM
  • I don't have time to replicate your cpu issue. Give me a little time and I will present the code I spoke of.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 1, 2020 2:36 PM
  • Here is an example 

    https://github.com/karenpayneoregon/visual-basic-getting-started/tree/master/MySettingsAlternate


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, October 1, 2020 3:34 PM
  • Thanks Karen! Appreciated
    Thursday, October 1, 2020 4:03 PM