1

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
SSpoke
  • 5,656
  • 10
  • 72
  • 124

1 Answers1

1

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.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • works perfect thanks, there still is little bug but it seems to fix it self up after, the bug is it still uses the old system if you drag mouse it will automatically for a second go to the very end then change back to where your mouse is. Anyway to disable the original TrackBar moving system? – SSpoke Jul 01 '13 at 22:38
  • ya you have to hold down the mouse button while moving in between two of those spacers if its more to the left it will go to value=0 if more then center it will go to max value. I've added MouseUp it makes it more rarer still happens though. And another bug I discovered the dblValue can sometimes become negative some bad calculation most likely the `e.X` fixed that bug with `If dblValue < 0 OrElse dblValue > tbTest.Maximum Then Return` and then another bug found the dblValue will never hit the maximum value. If I do `Math.Ceiling` on equation it will mess up all of them just for max – SSpoke Jul 01 '13 at 22:54
  • This seems to fix max problem not too bad `If dblValue > (tbTest.Maximum - 1) Then dblValue = tbTest.Maximum` is there any other TrackBar that has this built-in? – SSpoke Jul 01 '13 at 22:55
  • Only option I could find is the `TickFrequency` i've set it to `0` and `TickStyle` to `None` and it still is going to front or end even with no spacers in between. Thanks for all your help, most likely there is nothing we can do, o well it's decent anyways. – SSpoke Jul 01 '13 at 23:02
  • hii sspoke why when video play a trackbar cant follow cursor? – betrice mpalanzi Jul 23 '17 at 07:59