Question

I currently use this code to fix the bug where if you click somewhere on the Horizontal TrackBar it jumps to the middle then to the end of the TrackBar. So this code fixes that bug, which now jumps to the location you click.

But still a problem remains when I keep my mouse down and move it around the TrackBar the slider should follow but it just resets to beginning position, how do I make it follow right on top of cursor? would I need a timer control for that?

Private Sub tbTest_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbTest.MouseDown
    Dim dblValue As Double

    'Jump to the clicked location, bug FIX.
    dblValue = (Convert.ToDouble(e.X) / Convert.ToDouble(tbTest.Width)) * (tbTest.Maximum - tbTest.Minimum)
    tbTest.Value = Convert.ToInt32(dblValue)
End Sub
Was it helpful?

Solution

Make the method handle both the MouseDown() and MouseMove() events like this:

Private Sub tbTest_MovePointer(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbTest.MouseDown, tbTest.MouseMove
    If e.Button = Windows.Forms.MouseButtons.Left Then
        Dim dblValue As Double

        'Jump to the clicked location, bug FIX.
        dblValue = (Convert.ToDouble(e.X) / Convert.ToDouble(tbTest.Width)) * (tbTest.Maximum - tbTest.Minimum)
        tbTest.Value = Convert.ToInt32(dblValue)
    End If
End Sub

*Note the multiple events listed after the Handles keyword at the end of the first line. I also added a check to ensure the left mouse button is down.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top