10

I never needed to use the TRichEdit before, always used TMemo.

I'd like RichEdit autoscrolling to the end when I add text like TMemo. See how it looks after you add some data:

enter image description here

As you can see, the scroll bar remains at the top, but I'd like it stayed at the bottom as I added text.

I've 'googled' for solutions but I couldn't find a good and reliable one.

Daniel Grillo
  • 2,368
  • 4
  • 37
  • 62
  • I don't see the difference. I tried `Memo1.Lines.Add('test')` and `RichEdit1.Lines.Add('test')` and they behave the same way. You need to specify more details. – Andreas Rejbrand Jun 16 '11 at 13:01
  • @Andreas, I put more details. I hope to be clearer now. – Daniel Grillo Jun 16 '11 at 13:12
  • @Daniel: Apparently you do not add text by doing `RichEdit1.Lines.Add('text')`. Are you using `SelText := 'text'` instead? – Andreas Rejbrand Jun 16 '11 at 13:15
  • @Andreas, I'm using `RichEdit1.Lines.Add('text')` – Daniel Grillo Jun 16 '11 at 13:20
  • @Andreas: If we use RichEdit1.Lines.Add() method also, the output is same as above(In D2006) – Bharat Jun 16 '11 at 13:20
  • Yes, now I see! I tried `RichEdit1.Lines.Add('text')` in the `OnClick` of the form, and so the Rich Edit (or memo) had the focus all the time. And then both the Rich Edit and the memo scroll. But if the control *has not* focus (e.g., use a `OnClick` of a `TButton`), then only the memo scrolls! Interesting... – Andreas Rejbrand Jun 16 '11 at 13:23

3 Answers3

19

Try this code

RichEdit1.SetFocus;
RichEdit1.SelStart := RichEdit1.GetTextLen;
RichEdit1.Perform(EM_SCROLLCARET, 0, 0);
Daniel Grillo
  • 2,368
  • 4
  • 37
  • 62
Bharat
  • 6,828
  • 5
  • 35
  • 56
  • 1
    Thanks. It works! I have edit a little your answer, if you don't mind. – Daniel Grillo Jun 16 '11 at 13:29
  • 5
    @Daniel: I am not 100 % sure, but I suspect that `RichEdit1.GetTextLength` is **much** faster than `Length(RichEdit1.Text)`. The former only sends a `WM_GETTEXTLENGTH` message to the Rich Edit window (as Microsoft wants you to do), whereas the other first have to obtain the entire text as a string (potentially very slow?), and then reads the length cardinal of it (very fast). UPDATE: Sorry, you wanted to use `GetTextLen`, and Bharat used `Length`. I mixed you up! – Andreas Rejbrand Jun 16 '11 at 13:37
  • Well, it was BugFinder who wanted to use GtTextLen AFAICS, but this is all mixed up now. – Sertac Akyuz Jun 16 '11 at 13:50
  • I don't know if it's dependent on Windows or Delphi version, but with Delphi 2007 on Windows XP and 7 all I needed was the .Perform line. It worked even without the TRichEdit being focused. – Chad N B May 16 '13 at 14:48
  • Just wanted to add that using RichEdit1.Perform(EM_SCROLLCARET, 0, 0); by itself worked great for me (Delphi XE2) – KeyszerS Feb 09 '14 at 19:07
  • Yeah, I had to make some major changes based on this `SetFocus` requirement. What I was doing is a log, in which case everything on the form is disabled during writing the log. So I had to manually disable individual controls at a time, rather than the whole container (because you cannot set focus to disabled controls). – Jerry Dodge Nov 01 '15 at 17:19
11

Here is much easier and fast solution:

In OnChange event handler just:

SendMessage(RichEdit1.handle, WM_VSCROLL, SB_BOTTOM, 0);

Original answer from: https://www.experts-exchange.com/questions/21002277/Richedit-autoscrolling.html

Niki
  • 558
  • 4
  • 10
1

In short, you need to place the cursor at the end, or send code to scroll down.

Have you tried

Richedit1.SelStart :=RichEdit1.GetTextLength();
SendMessage(RichEdit1.handle, EM_SCROLLCARET,0,0);
BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • I tried and it didn't work. You have to put `RichEdit1.SetFocus` before. And at least in Delphi 2010 you have to use `GetTextLen` instead of `GetTextLength` – Daniel Grillo Jun 16 '11 at 13:33
  • Thats kinda irritating, Im surprised it needs focus. (the getTextLength was from memory, so Im not too surprised sorry) – BugFinder Jun 16 '11 at 14:01