How to detect whether scrollbar is at the very bottom?
Asked Answered
T

3

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

Thracophrygian answered 5/12, 2016 at 9:3 Comment(1)
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?Lysin
M
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;
Mafalda answered 5/12, 2016 at 10:14 Comment(3)
The book's title could also be: "Clever WinAPI solutions for Delphi".Thracophrygian
@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.Borborygmus
to complete this, to know if the scrollbar is at the top use IsScrollBarAtTop := Info.nPos = 0;Tigrinya
M
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.

Magdalenamagdalene answered 5/12, 2016 at 9:14 Comment(4)
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.Lysin
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...Mafalda
@SertacAkyuz, just tried with and without horisontal bar, and it works.Magdalenamagdalene
@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. +1Mafalda
C
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)

Collage answered 30/5, 2023 at 6:52 Comment(2)
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?Vail
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.Vail

© 2022 - 2024 — McMap. All rights reserved.