1

I am adding lines to a TRichEdit how do I keep focus on the line that has just been added?

For Idx := 1 to 1000 do
   RichEdit.Lines.add(IntToStr(Idx));

EDIT

I just what the bottom line of the richedit to show what was just added and all the other lines to scroll up

Argalatyr
  • 4,639
  • 3
  • 36
  • 62
Charles Faiga
  • 11,665
  • 25
  • 102
  • 139
  • I think "focus" is usually used for controls. Do you mean that you want to select the text that was just added (will result in pasting-over with the next operation), keep the insertion point on the last-added line (that will result in pasting one very long line) or perhaps you just mean you want to scroll to the bottom of the richedit? – Argalatyr Sep 28 '09 at 21:39
  • See also this answer: [Scrolling RichEdit without it having focus](http://stackoverflow.com/a/9757314/576719). – LU RD Mar 18 '12 at 10:17

2 Answers2

5

This should work, if you just want to scroll to the end of the richedit:

For Idx := 1 to 1000 do
begin
  RichEdit.Lines.add(IntToStr(Idx));
  RichEdit.SelStart := RichEdit.GetTextLen;
  SendMessage(RichEdit.handle, EM_SCROLLCARET,0,0);
end;

If this is not what you wanted, please see my comment above and clarify your question.

Argalatyr
  • 4,639
  • 3
  • 36
  • 62
3

You can try this

option 1) set the cursor when the loop finish

For Idx := 1 to 1000 do
   RichEdit.Lines.add(IntToStr(Idx));

RichEdit.SetFocus;
RichEdit.SelStart := RichEdit.Perform(EM_LINEINDEX, RichEdit.Lines.Count-1, 0) ;

option 2) set the cursor while the loop is executing

For Idx := 1 to 1000 do
Begin
RichEdit.Lines.add(IntToStr(Idx));
RichEdit.SetFocus;
End;

RichEdit.SelStart := RichEdit.Perform(EM_LINEINDEX, RichEdit.Lines.Count-1, 0) ;

Bye.

RRUZ
  • 134,889
  • 20
  • 356
  • 483