locked
How do I override Windows 10 System Setting for Mouse Scroll for certain controls? RRS feed

  • Question

  • Windows 10 allows you to set up your mouse to scroll by x lines or page.

    Personally, I enjoy this in a lot of cases. However, there are times it seems quite inconvenient and I'm not sure how to get around it in my own program. (So much easier to scream at support to fix it, not understanding the storm it might generate =D)

    In this particular case, I have a number of "NumericUpDown" controls, and they all automagically accept scroll events on mouseover without capturing focus. This would be quite fine if it weren't for the fact that each scroll event is generating a change by the number set in my system settings for "x lines".

    I've tried adding a scroll event handler, but it seems it isn't visible programmatically for "NumericUpDown" controls so the code is effectively ignored even though it doesn't throw an error. What I mean by this is: On the "Events" list for the control in the IDE, there is a field designated for the event, but when you click the associated drop down, no existing event handlers have populated the list there... you can add one to the text portion manually, but it does (or appears to do) nothing during execution.

    My reading has somewhat lead me to believe that I need to make/register (I'm sure that isn't the right terminology) a "delegate", but the example provided was fairly unrelated to my current train of thought and was basically unhelpful overall as regards understanding what it was doing and why this was helpful, or how to implement it in a more general way. Regardless, I could as easily be in left field staring at the stars.

    I'm fairly sure this is not such a challenging task, as I have many programs that do not heed the system settings when appropriate to do so. It is my hope that someone can not only tell me how to do this in my case, but also explain the process a bit better than the docs do so that I may apply it appropriately, at need, in the future.

    Thank you.

    Sunday, August 6, 2017 3:18 PM

Answers

  •  You can use the MouseWheel event to do this.  You can cast the MouseEventArgs class passed to this event,  to a HandledMouseEventArgs Class which will allow you to mark the message as Handled.  That will stop the NumericUpDown from responding to the scrolling event.  The Delta property of the MouseEventArgs class will be a positive number (usually 120) when the wheel was scrolled forward 1 click or a negative number (usually -120) if it was scrolled backward 1 click.  That will allow you to detect the scroll direction.

     Once you know the mouse wheel direction,  you can use several ways to adjust the value of the NumericUpDown.  You could add/subtract to or from the Value Property of the NUD.  You could also use the SendMessage or other Api functions to send vertical scroll messages to the NUD.  In the example below,  i took advantage of the NumericUpDown`s DownButton Method and UpButton Method which will increment the NUD Value by whatever the Increment Property value of the NUD is set to.

     With the Increment property of the NUD set to 1,  this will increment the Value up or down by 1 on each mouse wheel click.  Try this in a new form project with 1 NumericUpDown control added to the form.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            NumericUpDown1.Increment = 1
        End Sub
    
        Private Sub NumericUpDown1_MouseWheel(sender As Object, e As MouseEventArgs) Handles NumericUpDown1.MouseWheel
            DirectCast(e, HandledMouseEventArgs).Handled = True
            If e.Delta > 0 Then
                NumericUpDown1.UpButton()
            Else
                NumericUpDown1.DownButton()
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, August 6, 2017 5:06 PM
    • Marked as answer by Writeagame Sunday, August 6, 2017 5:50 PM
    Sunday, August 6, 2017 5:04 PM

All replies

  •  You can use the MouseWheel event to do this.  You can cast the MouseEventArgs class passed to this event,  to a HandledMouseEventArgs Class which will allow you to mark the message as Handled.  That will stop the NumericUpDown from responding to the scrolling event.  The Delta property of the MouseEventArgs class will be a positive number (usually 120) when the wheel was scrolled forward 1 click or a negative number (usually -120) if it was scrolled backward 1 click.  That will allow you to detect the scroll direction.

     Once you know the mouse wheel direction,  you can use several ways to adjust the value of the NumericUpDown.  You could add/subtract to or from the Value Property of the NUD.  You could also use the SendMessage or other Api functions to send vertical scroll messages to the NUD.  In the example below,  i took advantage of the NumericUpDown`s DownButton Method and UpButton Method which will increment the NUD Value by whatever the Increment Property value of the NUD is set to.

     With the Increment property of the NUD set to 1,  this will increment the Value up or down by 1 on each mouse wheel click.  Try this in a new form project with 1 NumericUpDown control added to the form.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            NumericUpDown1.Increment = 1
        End Sub
    
        Private Sub NumericUpDown1_MouseWheel(sender As Object, e As MouseEventArgs) Handles NumericUpDown1.MouseWheel
            DirectCast(e, HandledMouseEventArgs).Handled = True
            If e.Delta > 0 Then
                NumericUpDown1.UpButton()
            Else
                NumericUpDown1.DownButton()
            End If
        End Sub
    End Class
    


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Sunday, August 6, 2017 5:06 PM
    • Marked as answer by Writeagame Sunday, August 6, 2017 5:50 PM
    Sunday, August 6, 2017 5:04 PM
  • Thank you.

    Not sure I understand as much as I'd like, but this makes working sense at least.

    By extension, this is what I ended up with (and it works the way I hoped it would, so /o\ )

     	Private Sub NumericUpDown_MouseWheel(sender As Object, e As MouseEventArgs) Handles NumericUpDown9.MouseWheel, NumericUpDown10.MouseWheel, NumericUpDown11.MouseWheel, NumericUpDown12.MouseWheel, NumericUpDown13.MouseWheel, NumericUpDown14.MouseWheel, NumericUpDown15.MouseWheel, NumericUpDown16.MouseWheel, NumericUpDown17.MouseWheel, NumericUpDown18.MouseWheel, NumericUpDown19.MouseWheel, NumericUpDown20.MouseWheel
            DirectCast(e, HandledMouseEventArgs).Handled = True
            Dim thisNUD As NumericUpDown = TryCast(sender, NumericUpDown)
            If e.Delta > 0 Then
                thisNUD.UpButton()
            Else
                thisNUD.DownButton()
            End If
        End Sub

    So, I only need to write it once and add on all the NUD's I want it to work on. Perfect!

    Thanks again!

    • Edited by Writeagame Sunday, August 6, 2017 5:50 PM
    Sunday, August 6, 2017 5:48 PM