Textarea automatically scroll to bottom

Hi everyone,

I have a very simple question. How can I set a TextArea to automatically scroll to the bottom when new data is added?

You can get the string length of the TextArea.Text property and then get the line number for that character position - then set the TextArea.ScrollPosition property. I have a ‘trace window’ in an application that adds the text to display using the code below and scrolls as the trace information is added - it looks like:

Sub AddTrace(trace as string)
  TextArea1.AppendText(trace + EndOfLine)
  
  dim lastLine as integer = TextArea1.LineNumAtCharPos(TextArea1.Text.Len())
  
  if lastLine > 2 then
    lastLine = lastLine - 1
  end
  
  TextArea1.ScrollPosition = lastLine
End Sub

Perfect. Thanks Carl

I resume this old thread. The code proposed by Carl works on OSX but it does not on Linux (using the latest Xojo version). Has anybody else have this issue? workaround?

Thanks.

did you try to just scroll to an VERY large number?

TextArea1.ScrollPosition = 1000000

I’m using Carl’s code for the autoscroll. It works great in OS X, but in Windows when the text is at the bottom of the window it jumps to like another page to put in the new text. Is there something I can add to the above code to make a Windows version scroll like OS X ?

TextArea1.AppendText(yourText + EndOfLine) TextArea1.SelStart = TextArea1.Text.Len

Was looking at this again today and came up with a solution that actually works in Windows.

Dim strNL As String = Chr(133) //Use any ascii char here TextArea1.Text = ReplaceAll(TextArea1.Text, strNL, "") TextArea1.AppendText(yourText + EndOfLine + strNL) TextArea1.SelStart = TextArea1.Text.Len TextArea1.ScrollPosition = TextArea1.LineNumAtCharPos(InStr(TextArea1.Text, strNL))

TextArea1.ScrollPosition = TextArea1.LineNumAtCharPos(InStr(TextArea1.Text, strNL))

Note that I’ve found that setting .ScrollPosition to be very slow in large text fields containing a lot of text (especially on macOS) - so you may want to avoid calling this too often.