How to detect whether scrollbar is at the very bottom?

3k Views Asked by At

It is easy to detect whether the vertical scrollbar of a TScrollBox is at the very top or not:

IsScrollBarAtTop := ScrollBox1.VertScrollBar.Position = 0;

enter image description here

But how can I detect whether the vertical scrollbar of a TScrollBox is at the very BOTTOM or not?

enter image description here

3

There are 3 best solutions below

3
Sertac Akyuz On BEST ANSWER

You can retrieve scroll bar information through the API and determine if its at the bottom.

function IsScrollBarAtBottom(Box: TScrollBox): Boolean;
var
  Info: TScrollInfo;
begin
  Info.cbSize := SizeOf(Info);
  Info.fMask := SIF_POS or SIF_RANGE or SIF_PAGE;
  Win32Check(GetScrollInfo(Box.Handle, SB_VERT, Info));
  Result := Info.nPos >=  Info.nMax - Info.nMin - Info.nPage;
end;
4
LU RD On

From Vcl.Forms.TControlScrollBar.Range:

Range represents the virtual size (in pixels) of the associated control's client area. For example, if the Range of a form's horizontal scroll bar is set to 500, and the width of the form is 200, the scroll bar's Position can vary from 0 to 300.

IsScrollBarAtBottom :=  ScrollBox1.VertScrollBar.Position =
  (ScrollBox1.VertScrollBar.Range - ScrollBox1.ClientHeight);

If the range is less than the height of the scrollbox, the scrollbar is not visible.

2
Michal On

the important value is the position of the content ... ScrollBox.ViewportPosition I've been looking for it a while and it wasn't mentioned in any discussions, so I'm stating it.(Delphi 10.2, FMX)