9

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

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • A little 'out-the-box' idea.... Since scroll-bar is meant to be a derived abstract representation of where you are within _whatever you're scrolling_; perhaps a better approach is to look at where are in the underlying view. E.g. for a text editor/viewer: are you on the last line of the file? – Disillusioned Dec 05 '16 at 10:00

3 Answers3

13

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;
Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • 1
    The book's title could also be: "Clever WinAPI solutions for Delphi". – user1580348 Dec 05 '16 at 13:36
  • 2
    @user Delphi isn't really relevant here. This is a Win32 control and a Win32 answer. The code could be written in any language. VCL is a loose wrapper around win32 which makes this sort of thing easy. A lot of Delphi programmers are scared of other languages and won't read code written in any other language. This is a great weakness. Don't be afraid. Learn enough C++ to be able to read C++ Win32 sample code, and write simple C++ programs and you will have access to far more valuable information. – David Heffernan Dec 06 '16 at 07:46
  • to complete this, to know if the scrollbar is at the top use `IsScrollBarAtTop := Info.nPos = 0;` – user30478 Oct 05 '22 at 19:17
10

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.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • Would love to know if someone can confirm this works. It is conceptually the same as the WinAPI solution. But simpler and without digging down an onion layer. – Disillusioned Dec 05 '16 at 12:37
  • 1
    There's no direct correspondence between the box's height or clientheight with scrollable range. It would depend on button thickness/height, whether a horizontal bar exists, etc.. Not suggested... – Sertac Akyuz Dec 05 '16 at 12:42
  • 2
    @SertacAkyuz, just tried with and without horisontal bar, and it works. – LU RD Dec 05 '16 at 12:48
  • 1
    @LURD - You're right. I had a chance to look at code of TControlScrollBar, it takes into account button sizes in ControlSize and so on. +1 – Sertac Akyuz Dec 05 '16 at 19:30
0

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)

Michal
  • 1
  • Could you perhaps add a link to the docs along with a code snippet of how you could use your suggestion to answer the question as done in the other answers? – loonatick May 30 '23 at 13:00
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – loonatick May 30 '23 at 13:01