Firemonkey TMemo Scrolling

When you add lines to a TMemo in Delphi Applications, then you might want to scroll to the very end of that particular Memo, so that the newest lines are kept in view.

In FireMonkey it’s as easy as just calling GoToTextEnd:

procedure TFormMain.AddLog(AMessage: string);
begin
  Memo1.Lines.Add(AMessage);
  Memo1.GoToTextEnd;
end;

You might notice though, that when you clear the Memo at certain times, the scrollbars may get out of sync. To avoid that, you need to call ProcessMessages right after „clear“. Apparently the scrollbars get confused, when you add lines directly after clear command, because the actual position needs to be rendered first (which the ProcessMessages does).

procedure TFormMain.AddLog(AMessage: string);
begin
  if Memo1.Lines.Count > 100 then 
    begin
      Memo1.Lines.Clear;
      Application.ProcessMessages; //without this, scrollbars will show wrong size/position
    end;
  Memo1.Lines.Add(AMessage);
  Memo1.GoToTextEnd;
end;

No Comments

Post a Comment