Need to slow down a mouse wheel, witch is changing tabs in a TabControl.

Hi,

This code work fine, but just a little bit to fast, what would be the right way to slow down the speed?

    Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
        If CBool(Control.ModifierKeys And Keys.Shift) Then
            If e.Delta > 0 Then
                'Up
                If TabControl1.SelectedIndex < 9 Then
                    TabControl1.SelectedIndex = TabControl1.SelectedIndex + 1
                End If
            Else
                'Down
                If TabControl1.SelectedIndex > 0 Then
                    TabControl1.SelectedIndex = TabControl1.SelectedIndex - 1
                End If
            End If
        End If
    End Sub

Open in new window



Any help will be greatly appreciated

Best regards
NaanPLS
NaanPLSAsked:
Who is Participating?
 
Jaime OlivaresSoftware ArchitectCommented:
I had this problem before. You are performing an action for any wheel turn, no matters how much.
Your first approach would be to perform an action just when the delta is greater than some value, but this would work just when you perform a fast turn.
A better approach is to accumulate the deltas until they surpasses some threshold value. Something like (not tested):

Private accumDelta as Integer  'Declare this field somewhere
Private Const deltaThreshold as Integer = 10

Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
    If CBool(Control.ModifierKeys And Keys.Shift) Then
        If (e.Delta > 0 And accumDelta > 0) Or (e.Delta < 0 And accumDelta < 0) Then
              accumDelta = accumDelta + e.Delta
        Else
              accumDelta = e.Delta
        EndIf
        If accumDelta > deltaThreshold  Then
             If TabControl1.SelectedIndex < 9 Then
                 TabControl1.SelectedIndex = TabControl1.SelectedIndex + 1
             End If
             accumDelta = accumDelta - deltaThreshold
        EndIf
        If accumDelta < -deltaThreshold  Then
             If TabControl1.SelectedIndex > 0 Then
                 TabControl1.SelectedIndex = TabControl1.SelectedIndex - 1
             End If
             accumDelta = accumDelta +  deltaThreshold
        EndIf
    End If
End Sub

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
According to the docs, you should act on a delta greater than 120:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousewheel.aspx

    "The UI should scroll when the accumulated delta is plus or minus 120."
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.