1 Reply Latest reply on Sep 16, 2007 8:15 PM by theLoggerGuy

    TextArea scroll position (maxVerticalScrollPosition)

    Ansury Level 3
      This is intended to keep scrolling to the LAST available line in the text area when a line is added (click button), but (unless I'm doing something wrong), it always seems to lag behind a little. I'm not sure why. I would think that doing this would set the scroll position correctly:

      text.verticalScrollPosition = text.maxVerticalScrollPosition;

      example below:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

      <mx:TextArea id="text" height="110" width="100%" htmlText="{t}" scroll="onScroll();"
      editable="false" selectable="false"/>

      <mx:Script>
      <![CDATA[
      [Bindable]
      private var t:String = "test1<br>test2<br>test3<br>test4<br>test5<br>test6<br>test7<br>";

      private function onScroll():void {
      trace("max: " + text.maxVerticalScrollPosition +
      " pos: " + text.verticalScrollPosition);
      }

      private function test():void {
      t += "NEW LINE<br>";
      text.verticalScrollPosition = text.maxVerticalScrollPosition;
      trace("max: " + text.maxVerticalScrollPosition +
      " pos: " + text.verticalScrollPosition);
      }
      ]]>
      </mx:Script>
      <mx:Button x="94" y="218" label="Button" click="test();"/>

      </mx:Application>

      What's wrong here?
        • 1. Re: TextArea scroll position (maxVerticalScrollPosition)
          theLoggerGuy
          You have a race condition of when the display is updated and when you make the call to jump to the last line. Use the callLater function like this:

          private function test():void {
          t += "NEW LINE<br>";
          callLater( scrollToEnd );
          }

          private function scrollToEnd( ): void{
          text.verticalScrollPosition = text.maxVerticalScrollPosition;
          trace("max: " + text.maxVerticalScrollPosition + " pos: " + text.verticalScrollPosition);
          }