Muhammad Alkarouri
Muhammad Alkarouri

Reputation: 24662

Keeping the caret at the end of text in a rich edit

I am writing an editor in Delphi (2009) using a TRichEdit component. The editor is append-only, in the sense that the caret must be at the end at all times, while maintaining the ability to copy using the mouse from elsewhere in the component.

The way it is working at the moment is by moving the caret to the end whenever something is written, but is it possible to make the caret not follow the mouse when clicking at other parts of the text?

Upvotes: 1

Views: 2147

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595857

No. The caret must move in order for the user to make selections with the mouse or keyboard. You will have to move the caret to the end each time you insert new text. You should probably keep and restore the user's current caret position during each insertion as well, eg:

procedure TForm.AppendText(const S: String);
var
  OldCharRange, NewCharRange: TCharRange;
begin
  SendMessage(RichEdit1.Handle, EM_EXGETSEL, 0, LParam(@OldCharRange));
  try
    NewCharRange.cpMin := RichEdit1.GetTextLen;
    NewCharRange.cpMax := NewCharRange.cpMin;
    SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LParam(@NewCharRange));
    RichEdit1.SelText := S;
  finally
    SendMessage(RichEdit1.Handle, EM_EXSETSEL, 0, LParam(@OldCharRange));
  end;
end;

Upvotes: 3

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108948

No, it is not possible. You have to move the caret to the end when the user types something.

Upvotes: 3

Related Questions