dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] NumericUpDown Scroll Wheel Increments

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2020
    Posts
    19

    Resolved [RESOLVED] NumericUpDown Scroll Wheel Increments

    To help override the system mouse wheel increments (which can make the scroll skip by 3-5 steps as opposed to just one), can the following be applied to ALL numercUpDown controls without writing one for each control?

    Code:
    Private Sub NumericUpDown1_MouseWheel(sender As Object, e As MouseEventArgs) Handles NumericUpDown1.MouseWheel
            NumericUpDown1.Increment = 1D / SystemInformation.MouseWheelScrollLines
        End Sub

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,912

    Re: NumericUpDown Scroll Wheel Increments

    Yes, a couple of different ways.

    For one thing, do you see that Handles clause on the end? You can have lots of things in there, so it could be:

    Handles NumericUpDown1.MouseWheel, NumericUpDown2.MouseWheel, NumericUpDown3.MouseWheel....etc.

    Now, considering the number of controls you mentioned earlier, you certainly wouldn't gain much of anything if you were to type all of that out for each control, but there are easier ways.

    1) If you select a bunch of controls in the form designer, then you can go to the properties and select the events (a lightning bolt icon at the top of the properties window), and you'll have the events common to all the selected control. Click the MouseWheel event in there, and you'll get the one sub with the Handles...looking like holy hell, since there'll be every single control you selected on there, which sounds like it might take up a few rows, in your case, though I think it won't wrap, so it will be one Brobdingnagian line.

    2) If you have your controls in a collection, which they likely will be, then you can write the one sub, then use AddHandler to assign the event handler to each control in a loop. You may or may not realize that your controls are in a collection, but at the very least, they are in the .Controls collection of the container on the form. That may be the form itself, or a panel, groupbox, tab page, or any other sub-control that they might be found in.

    If you take that second route, then also call RemoveHandler when you are done with the form. Whether or not this is strictly necessary depends on other aspects of your form (such as whether or not anybody will ever come back to it within one run of the program), but why wonder? If you added the handlers with AddHandler, RemoveHandler is oddly the same, so you could just copy the code into the FormClosing event and switch Remove in place of Add.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2020
    Posts
    19

    Re: NumericUpDown Scroll Wheel Increments

    I have tried this and it only works for the first control:

    ... Handles NumericUpDown1.MouseWheel, NumericUpDown2.MouseWheel, numericUpDown3.MouseWheel ...

    I am going to try method #1 and see how that goes.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,913

    Re: NumericUpDown Scroll Wheel Increments

    The sender identifies what object caused the event. You cast the object as an instance of the Class and use it.
    I assume you still had NumericUpDown1 in your sub, which will only address the one.
    Code:
    Private Sub NumericUpDown1_MouseWheel(sender As Object, e As MouseEventArgs) Handles NumericUpDown1.MouseWheel, Handles NumbericUpDown2.MouseWheel
            Dim nud as NumericUpDown = DirectCast(sender, NumericUpDown)
            nud.Increment = 1D / SystemInformation.MouseWheelScrollLines
    End Sub
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2020
    Posts
    19

    Re: NumericUpDown Scroll Wheel Increments

    Unfortunately, MouseWheel does not appear to be an event using method 1. On to #2...

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,912

    Re: NumericUpDown Scroll Wheel Increments

    That doesn't make sense unless you included a control for which the MouseWheel is NOT an event. For the NUD, it MUST be an event, because you're already handling that event.

    Still, option #2 is the way I would have gone anyways. If you have more than about half a dozen controls that you'll be sharing the event handler for, then that Handles clause will be super ugly. I was under the impression that you had a grid of controls.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2020
    Posts
    19

    Re: NumericUpDown Scroll Wheel Increments

    @passel, your answer (with a quick edit) worked like a charm!

    Code:
    Private Sub NumericUpDown1_MouseWheel(sender As Object, e As MouseEventArgs) Handles NumericUpDown1.MouseWheel, NumericUpDown2.MouseWheel, NumericUpDown3.MouseWheel, NumericUpDown4.MouseWheel, NumericUpDown5.MouseWheel, NumericUpDown6.MouseWheel, NumericUpDown7.MouseWheel, NumericUpDown8.MouseWheel
        Dim nud As NumericUpDown = DirectCast(sender, NumericUpDown)
        nud.Increment = 1D / SystemInformation.MouseWheelScrollLines
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width