Search code examples
vb.netscrollmousemousewheeltrackbar

TrackBar custom Small & Large Change on Mouse Move / Scroll Up & Down in VB.NET


Well I'm trying something like that:

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
    Dim scv As Int32 = TrackBar1.Value
    Dim uni As [String] = "ms"

    Select Case scv
        Case Is > 1000
            scv = scv \ 1000
            uni = "s"
            sender.SmallChange = 1000
        Case Is > 100
            sender.SmallChange = 50
        Case Is > 50
            sender.SmallChange = 50
        Case Is > 25
            sender.SmallChange = 25
        Case Is > 10
            sender.SmallChange = 15
    End Select

    Label4.Text = (scv & uni).ToString
End Sub

But its onyl works with the arrow keys < and >, if I try it with the mouse move or the mouse scroll wheel, doesn't work. And... Only work if I go from Left to Right...

What I have to do? :(


Solution

  • Try something like this out...

    Private Sub TrackBar1_Scroll(sender As Object, e As EventArgs) Handles TrackBar1.Scroll
        Dim bar As TrackBar = DirectCast(sender, TrackBar)
    
        Select Case bar.Value
            Case Is >= 1000
                bar.SmallChange = 1000
            Case Is > 100
                bar.SmallChange = 50
            Case Is > 50
                bar.SmallChange = 50
            Case Is > 25
                bar.SmallChange = 25
            Case Is > 10
                bar.SmallChange = 15
        End Select
    
        Dim discrete As Integer = TrackBar1.Value \ TrackBar1.SmallChange
        Dim Value As Integer = discrete * bar.SmallChange
        bar.Value = Math.Min(Math.Max(bar.Minimum, Value), bar.Maximum)
    
        Label4.Text = IIf(bar.Value >= 1000, bar.Value \ 1000, bar.Value) & IIf(bar.Value >= 1000, "s", "ms")
    End Sub