Textarea automatically scroll to bottom

  1. 4 years ago

    Joshua W

    16 Sep 2013 Pre-Release Testers, Xojo Pro Germany

    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
  2. Carl C

    16 Sep 2013 Pre-Release Testers, Xojo Pro Answer

    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
  3. Joshua W

    16 Sep 2013 Pre-Release Testers, Xojo Pro Germany

    Perfect. Thanks Carl

  4. 2 years ago

    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.

  5. Giulio M

    1 Jul 2015 Pre-Release Testers Europe (Rome, Italy)

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

    TextArea1.ScrollPosition = 1000000

  6. last year

    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 ?

  7. 11 months ago

    Geoff H

    23 Jan 2017 Pre-Release Testers, Xojo Pro USA
    Edited 11 months ago

    @Bill H 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
  8. 5 months ago

    Geoff H

    Jul 10 Pre-Release Testers, Xojo Pro USA
    Edited 5 months ago

    @Bill Haag 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 ?

    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))
  9. Michael D

    Jul 16 Pre-Release Testers, Xojo Pro
    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.

or Sign Up to reply!