ScrollBy
The ScrollBy function scrolls the view relative to the current position:
procedure ScrollBy(const Dx, Dy: Single);
Dx and Dy specify offsets to the values of the horizontal and vertical scroll bars assigned to the control:
- Dx is the change in pixels horizontally (along the X axis)
- Dy is the change in pixels vertically (along the Y axis)
If my current Y position is 100 and I want to move upwards to the top, then I would invoke:
ScrollBy(0, -100);
To move downward I would need to supply a positive value:
ScrollBy(0, 100);
Similarly for scrolling left (Dx is a negative value), and scrolling right (Dx is a positive value).
Quick Reference
| Direction You Want to Go | Content Movement | ScrollBy Sign | Example Code |
|---|---|---|---|
| Up (Towards Top) | Moves Down | Negative (-) | ScrollBy(0, -100); |
| Down (Towards Bottom) | Moves Up | Positive (+) | ScrollBy(0, 100); |
| Left (Towards Start) | Moves Right | Negative (-) | ScrollBy(-100, 0); |
| Right (Towards End) | Moves Left | Positive (+) | ScrollBy(100, 0); |
ViewportPosition
Another useful property is the ViewportPosition.
To determine your current position:
var y := Scroller.ViewportPosition.Y;
var x := Scroller.ViewportPosition.X;
To scroll to the top:
Scroller.ScrollBy(0, -Scroller.ViewportPosition.Y);
An alternative is to just set the ViewportPosition property absolutely:
Scroller.ViewportPosition := PointF(0, 0);
Content Alignment
Finally, if the contents are not aligned correctly:
Scroller.RealignContent;
For more information, please see here.