Results 1 to 3 of 3

Thread: How to do smooth scrolling?

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    8

    Question How to do smooth scrolling?

    I have a form with controls that are created in runtime that can become larger than the form window (and easily larger than the screen), so want the user to be able to scroll smoothly when this happens.

    By smooth scrolling I mean active scrolling while the mouse button is pressed. Just like when you scroll on a browser. Currently I'm using AutoScroll and that only scrolls after the mouse button is released. I am not scrolling text - only controls that can vary in number and dimensions.

    Have tried putting a call to Invalidate and Refresh in the Scroll event, but this does nothing.
    Have tried using AutoScroll on the main form and in a panel in the form with no change.
    Have confirmed that the Scroll event is being called in both cases as the mouse moves.

    Searched on google for 2 hours and tried every solution I could find. None have worked.

    What am I missing? This should be child's play.

    Windows 10
    VB.Net on Visual Studio 2015

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

    Re: How to do smooth scrolling?

    It doesn't HAVE to be easy. For example, I don't even know what you mean about active scrolling while the mouse button is pressed. I scroll a browser with the mouse wheel. No scrolling happens when the mouse button is pressed, unless you mean on the scrollbar.

    If you DO mean scrolling on the scrollbar, it may be that you'll have to make it work yourself. You'd be on the right track with the Scroll event, but you're thinking about it a bit wrong with the Invalidate/Refresh. A scrollbar doesn't scroll the screen. Yeah, it seems like it does, but it doesn't. The screen is just the screen. It shows some number of pixels, and that's all it shows. There is no "larger than the screen" as far as the screen is concerned. So, when you are talking about a form that is larger than the screen, you are really talking about the screen being a view into a much larger canvas. The view shows from 0,0 to whatever the size of the screen is. Anything that is in that area is visible.

    Suppose you have a screen of 1000 x 1000 (which you don't, but it's a nice round number). Any object that fits fully within the rectangle defined by (0,0) - (1000,1000) is visible. Anything that is partially within that area is partially visible, and anything outside that area is not visible.

    If you have a form that is 2000 x 2000, then only a quarter of it is visible within the screen. Items in the form range from (0,0) to (2000,2000), but those aren't screen coordinates, those are form coordinates (often considered world coordinates). To show any particular item, the world coordinates have to shift such that they are visible. For example, if you have an object that is found at (1000,1500), it is off the screen to the right and off the screen to the bottom. For that to be visible, you need to shift it so that it is on the screen, so you have to move it's Top and Left properties such that they are within the screen area. For example, if you subtracted 1000 off of both the Top and Left, the item would appear at (0,500), and be fully visible. Of course, it would also appear on top of whatever was there.

    So, what you have to do, is whenever the scrollbar is moved, change ALL the Top and Left properties for ALL the elements on the screen. There are a few ways to do this. In that example, the form was twice as high as the screen. So you could have a scrollbar with a range of 1000. If this is a vertical scrollbar, whenever the Scroll event is raised, subtract the value of the scrollbar from the Top property of every control on the form. Therefore, if the scrollbar is at 0, you are subtracting 0 from the Top property, which will leave them all where they were placed. If you move the scrollbar to 100, you will subtract 100 from the .Top property of each control. Some will then move off the top of the screen, others will move onto the bottom of the screen, and so forth. By subtracting the scroll amount from ALL the .Top properties you will cause them all to move, but as long as you still have your finger on the button, you may not see them move. You'll have to try that out. Offhand, I can't remember whether that will work, or not. It very well may. The UI will have to have time to redraw the controls at the new positions, and I think it will. If not, then you'd have to call .Refresh on each control. You really don't want to do that unless you have to, as .Refresh forces the element to stop and draw at that moment. This may make the scrolling look smoother, but it is even more likely to make it look terrible, and cause the program to stutter.

    That's the basics of it. As you move the vertical scrollbar, subtract the scroll value from the .Top property. As you move the horizontal scrollbar, subtract the scroll value from the .Left property. Do this for all elements that are directly on the form. If you have items in a panel, then shift the panel, which is the part that is directly on the form. Don't be shifting the elements on the panel, as that would shift them on the panel, not on the form, which wouldn't be right.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    8

    Re: How to do smooth scrolling?

    Ok, thanks. I was hoping to not have to implement this functionality manually since it's so commonly used.
    I don't know any programs that only scroll after the mouse button is released and have used just about every Microsoft product that exists, so I assumed it would be the default behaviour.

    Anyway, it's not, so wishing it to be won't change anything. Time to make my own solution and reinvent the wheel...

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width