Recent

Author Topic: [SOLVED] How to scroll a TMemo to it's end (Windows+Linux)  (Read 622 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 1064
[SOLVED] How to scroll a TMemo to it's end (Windows+Linux)
« on: February 04, 2026, 05:41:06 pm »
When searching the Forum for this question, I found endless matches, but unfortunately none of them worked for me:

Code: Pascal  [Select][+][-]
  1. procedure showMemo(SL: TStringList);
  2.    var MM: TMemo;
  3.    begin
  4.    MM:=Form1.Memo1; {abbreviation}
  5.    MM.Lines.Assign(SL); {assigns a StringList to TMemo 'MM'}
  6.  
  7. // MM.ScrollBars:=ssVertical;
  8. // MM.VertScrollBar.Position:=MaxLongInt;
  9.  
  10. // MM.SelStart:=length(MM.Lines.Text);
  11. // MM.SelStart:=length(MM.Lines.Text)-1;
  12. // MM.SelStart:=MaxLongInt;
  13. // MM.SelLength:=0; {used always after a 'MM.SelStart:=...'}
  14.  
  15. // MM.CaretPos := Point(0, MM.Lines.Count-1);
  16.  
  17.    Form1.ShowModal; {shows the Form which contains TMemo 'MM'}
  18.    end;
  19.  
As you can see, I assign the contens of a StringList to a TMemo 'MM', then I try to scroll it to the end (but this does nothing) and then show the Form which contains TMemo 'MM'. Result: the Form with the Memo ist displayed and the content of the Memo is correct (the content of the StringList), but the Memo shows it's beginning and not it's end. A vertical scrollbar is displayed.

I tried all of the above alternatives, one by one (only 'MM.SelLength:=0' always after one of the 'MM.SelStart:=...').

Until now I only tried on Linux (Ubuntu 24.04 GTK2 with Lazarus 3.4.0 and FPC 3.2.2).
I want solutions for Windows and Linux.
Thanks in advance.
« Last Edit: February 05, 2026, 03:36:51 pm by Hartmut »

cdbc

  • Hero Member
  • *****
  • Posts: 2604
    • http://www.cdbc.dk
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #1 on: February 04, 2026, 06:33:16 pm »
Hi
You can try this:
Code: Pascal  [Select][+][-]
  1. MM.Selstart:= UTF8Length(MM.Caption);
I do see, that you tried that, but here with me it works as advertized...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Hartmut

  • Hero Member
  • *****
  • Posts: 1064
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #2 on: February 04, 2026, 06:45:42 pm »
Thanks Benny, but unfortunately it does not work (as the other 'MM.Selstart:=...' I tried before).

cdbc

  • Hero Member
  • *****
  • Posts: 2604
    • http://www.cdbc.dk
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #3 on: February 04, 2026, 07:27:38 pm »
Here you go.
Works like a charm on PCLinuxOS with QT6, got no way of testing on GTKx
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1572
    • Lebeau Software
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #4 on: February 04, 2026, 09:58:10 pm »
I don't know if this works the same for FPC/LCL, but in Delphi VCL on Windows I use this:

Code: Pascal  [Select][+][-]
  1. Memo1.SelStart := Memo1.GetTextLen;
  2. Memo1.SelLength := 0;
  3. // alternatively:
  4. // var len: Integer := Memo1.GetTextLen;
  5. // Memo1.Perform(EM_SETSEL, len, len);
  6.  
  7. Memo1.Perform(EM_SCROLLCARET, 0, 0); // <-- the magic happens here
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Hartmut

  • Hero Member
  • *****
  • Posts: 1064
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #5 on: February 05, 2026, 10:21:58 am »
Thanks Remy Lebeau. As said my current problem is on Linux. Because there I did not find EM_SETSEL and EM_SCROLLCARET, I stole them from Windows (don't know if this is legal) and tried this 2 alternatives:

Code: Pascal  [Select][+][-]
  1. const EM_SCROLLCARET = 183; {from /.../fpcsrc/rtl/win/wininc/messages.inc}
  2. MM.SelStart := MM.GetTextLen;
  3. MM.SelLength := 0;
  4. MM.Perform(EM_SCROLLCARET, 0, 0); // <-- the magic happens here
and:
Code: Pascal  [Select][+][-]
  1. const EM_SETSEL = 177;      {from /.../fpcsrc/rtl/win/wininc/messages.inc}
  2.       EM_SCROLLCARET = 183; {from /.../fpcsrc/rtl/win/wininc/messages.inc}
  3. var len: Integer;
  4. len:=MM.GetTextLen;
  5. MM.Perform(EM_SETSEL, len, len);
  6. MM.Perform(EM_SCROLLCARET, 0, 0); // <-- the magic happens here

Unfortunately both alternatives did not work (did not scroll).



Thanks a lot Benny for your demo. The good news is, it works (Linux Ubuntu 24.04 GTK2 with Lazarus 3.4.0 and FPC 3.2.2). But you have a main difference: you scroll in Button3Click(), which is during ShowModal(), while I need scrolling before calling ShowModal() as showed in my 1st post, because I want an automatic scroll and don't want the need to press a button each and every time to see the end of the Memo.

So I modified your demo by adding a FormCreate-Event:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2.    begin
  3.    writeln('FormCreate');
  4.    Memo1.Lines.LoadFromFile('unit1.pas');
  5.    Memo1.SelStart:= UTF8Length(Memo1.Caption);
  6. // Memo1.SelLength:=0; // did not help
  7.    end;

Unfortunately now your demo does not work anymore.

In my real code (which is a common unit, used by many programs):
 - the Form is created dynamically by CreateNew(Application)
 - then everything else is initialized
 - then the data is assigned from a StringList 'SL' to the Memo 'MM' via MM.Lines.Assign(SL)
 - then ShowModal() is called.

How can I cause a scroll to the Memo's end before calling ShowModal() - on Windows+Linux?

CM630

  • Hero Member
  • *****
  • Posts: 1607
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #6 on: February 05, 2026, 10:43:23 am »
Have you tried TRichMemo?
Лазар 4,4 32 bit (sometimes 64 bit); FPC3,2,2

Hartmut

  • Hero Member
  • *****
  • Posts: 1064
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #7 on: February 05, 2026, 10:57:59 am »
Hello CM630, I tried TRichMemo earlier in another context and faced a lot of problems.

In my real code (which is a common unit, used by many programs)

As said, I'm searching for a solution for a common unit, used by many programs, where I don't want to set everything upside down by replacing a TMemo by a TRichMemo - which a) would require endless effort for many many tests (double for Windows and for Linux) and would b) for sure cause a couple of other problems - even if scrolling would work there before calling ShowModal().

cdbc

  • Hero Member
  • *****
  • Posts: 2604
    • http://www.cdbc.dk
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #8 on: February 05, 2026, 10:59:47 am »
Hi
Hmmm... TMemo reacts to / manipulates the 'Viewport' when visible...
· Take a look at 'Application.QueueAsyncCall'
· Create a 'procedure HandleAsync(aData: ptrint);'
·· in this proc you call "MM.SelStart:= UTF8Length(MM.Caption);"
· Create a 'FormShow' event-handler
· In that handler call QueueAsyncCall(@HandleAsync,0), to get a small delay

The above ought to do the trick  ;D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Zvoni

  • Hero Member
  • *****
  • Posts: 3246
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #9 on: February 05, 2026, 11:23:57 am »
Thanks a lot Benny for your demo. The good news is, it works (Linux Ubuntu 24.04 GTK2 with Lazarus 3.4.0 and FPC 3.2.2). But you have a main difference: you scroll in Button3Click(), which is during ShowModal(), while I need scrolling before calling ShowModal() as showed in my 1st post, because I want an automatic scroll and don't want the need to press a button each and every time to see the end of the Memo.

So I modified your demo by adding a FormCreate-Event:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2.    begin
  3.    writeln('FormCreate');
  4.    Memo1.Lines.LoadFromFile('unit1.pas');
  5.    Memo1.SelStart:= UTF8Length(Memo1.Caption);
  6. // Memo1.SelLength:=0; // did not help
  7.    end;

Unfortunately now your demo does not work anymore.

In my real code (which is a common unit, used by many programs):
 - the Form is created dynamically by CreateNew(Application)
 - then everything else is initialized
 - then the data is assigned from a StringList 'SL' to the Memo 'MM' via MM.Lines.Assign(SL)
 - then ShowModal() is called.

How can I cause a scroll to the Memo's end before calling ShowModal() - on Windows+Linux?

*sigh*.... and have you tried the OnShow-Event instead of the OnCreate, hmm?
If something works DURING the Form is shown, but not before........

Quote
In my real code (which is a common unit, used by many programs):
 - the Form is created dynamically by CreateNew(Application)
 - then everything else is initialized
 - then the data is assigned from a StringList 'SL' to the Memo 'MM' via MM.Lines.Assign(SL)
 - then ShowModal() is called.
 - In OnShow do the Scrolling

Alternative might be OnActivation (since that one fires after OnShow), though with a Blocking-Variable "FirstRun" or something like that
« Last Edit: February 05, 2026, 11:29:59 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Hartmut

  • Hero Member
  • *****
  • Posts: 1064
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #10 on: February 05, 2026, 01:16:14 pm »
Short intermediate info: the solution from cdbc with QueueAsyncCall() in FormShow-Event works on Linux! Will test on Windows soon and report then.

@Zvoni: yes, I had already tried OnShow-Event and OnActivation before I started this Topic, but both had not worked (without QueueAsyncCall).

Hartmut

  • Hero Member
  • *****
  • Posts: 1064
Re: How to scroll a TMemo to it's end (Windows+Linux)
« Reply #11 on: February 05, 2026, 03:36:11 pm »
Meanwhile I tested, the solution from cdbc with QueueAsyncCall() in Event FormShow() works on Windows too. The problem is solved.
Thanks a lot to all who tried to help me, especially Benny.

 

TinyPortal © 2005-2018