How do I scroll to the bottom of a TextArea with code?
Because macOS hides the scrollbar by default I wanted to make a widget to show that more text is available when starting my app the first time.
I set the VerticalScrollPosition to 1000 to get the max scrollposition. There are 2 buttons to increase and decrease the scrollpostion. Then I have a timer to enable or disable the buttons:
if currentScrollPosition = 0 then
PBUp.setEnabled(False)
PBDown.setEnabled(True)
ElseIf currentScrollPosition = maxScrollPosition then
PBUp.setEnabled(True)
PBDown.setEnabled(False)
Else
PBUp.setEnabled(True)
PBDown.setEnabled(True)
end if
Now the problem:The down button gets disabled before the TextArea has been scrolled to the bottom:
I do not like the answer I am writing, but this is a solution: use a declare API (it certainly exists) to enable the Always (toujours below) RadioButton.
dim currentScrollPosition as Integer = TextArea1.VerticalScrollPosition
if isOpening then
maxScrollPosition = currentScrollPosition
TextArea1.VerticalScrollPosition = 0
isOpening = False
Return
end if
If I add an EndOfLine to my text then the buttons are okay.
My suggestion would be to use the multi-languages feature of Mac OS. For instance, whenever I reply to this forum (be it a screenshot or quoting the UI as text), I always go to System Preferences→Language & Region and put English to the top. Then I quit and relaunch the application I want to quote (if it’s System Preferences itself, proceed with caution to not answer “Restart now”, which would restart the computer), I take the screenshot or write down the text, go back to Language & Region and put English where it was.
That way (1) you know how to name the path you took to go there and (2) it’s much more easy for English readers to know what you’re showing.
I invite you to try.
I’m not exactly sure right now, but I think you can achieve what you want by playing with VerticalScrollPosition, InsertionPosition and LineNumber.
Something along those thoughts:
(1): me.LineNumber(me.Text.Length) gives you the index of the last line.
(2): me.LineNumber(me.InsertionPosition(me.width-1,me.height-1))-me.LineNumber(me.InsertionPosition(0,0)) tells you how many lines are shown on the TextArea (when there’s only a single style).
So if you set VerticalScrollPosition (=the first visible line) to (1)-(2) you’d get the right value.
I know it’s awkward, but I recall having seen acceptable results in the past.