I have a TextArea that is used to implement a sort of terminal window. Text is added using AddText method and it can happen when not under the control of the user. I would like to shift the vertical scroll bar position automatically so that the most recently added text is visible at the bottom of the TextArea if there is enough text to fill the TextArea. The text is styled text.
At this point, I am rather stumped. Can anyone describe how to do this?
Oregon Research Electronics
set the position of the vertical scrollbar to the last character when you append text to the control:
Me.VerticalScrollPosition = <pass the length of the text here ?>
Hello, Emile -
Is the length of the text, the length in number of characters (e.g. TextArea.Value.Length) or number of lines?
I am not accustomed to this API 2 blah blah.
Note that I do not found the instructions in the LR, but auto-complete helped me to discovers the VerticalScrollPosition property.
Thank you Emile -
That does it. I have no clue about why value.length should be the key but it works, so that is good enough for now.
Like you, I don’t have much use for API 2 but I don’t see much choice into the future. Except that, by then, it will probably be API 99.
Normally, the VerticalScrollPosition value is about the number of visible lines (IIRC, the index of the line that appears at the very top of the text area). But if your value is greater than the maximum possible value (i.e. the total number of lines in the control minus how many lines are visible), the scroll position will go to its maximum (like when you set a progress bar’s value beyond its maximum, it’ll go to the maximum).
Since you always have more characters than lines, setting the length of the text for the scroll position behaves as explained.
Thank you for the explanation, Arnaud. That helps…
So, I was not so right. Sorry. I mixed things in my mind. I will check if setting the cursor at the end of the TextArea made a scroll to the end.
You are right. It works nicely. But, maybe not for the reason that you expected.
Need to bring this back to life.
Emile’s solution works like a charm on MacOS. But on Windows (testing Win10) using 2019 R3.2, that method shifts the last line to the top of the screen. To be fair, the LR says:
Use this property to determine the first visible line; set this property to scroll the control. Setting it to the last line or beyond the last line will simply move the vertical scrollbar’s thumb to the bottom, not necessarily displaying the last line at the top of the control.
Can someone either suggest a general cross-platform method to shift text added to a TextArea using the AddText method so that the last line is visible at the bottom of the TextArea OR a Windows-specific method to do the same?
I have tried adjusting the thumb position using the TextArea.height property and the TextArea.LineHeight property but I keep getting 0.0 for the latter.
Oregon Research Electronics
Not nice but it works WIN 10 - Xojo 2019 R3.2
textAreaTest.AddText(EndOfLine.Windows + "text")
var amountOfLineNumbers as integer = textAreaTest.LineNumber( textAreaTest.Value.Length )
for i as integer = 0 to amountOfLineNumbers
textAreaTest.VerticalScrollPosition = i
Thanks, Wolfgang -
I’ll take “works” over “nice” any day.