Home > Enterprise >  How can I know if all the text is selected in a TRichEdit when the text has three or more lines?
How can I know if all the text is selected in a TRichEdit when the text has three or more lines?

Time:10-06

I have a TRichEdit with some RTF in it (text with formatting only) and I want to know if all the content of the TRichEdit is selected. To do so, I do:

var AllSelectd: Boolean;
//...
AllSelectd := MyRichEdit.SelLength = MyRichEdit.GetTextLen;

which works fine, except when the content has three lines or more. With zero to two lines, everything is fine. As soon as I reach three lines in my TRichEdit, the code above no longer works (MyRichEdit.SelLength < MyRichEdit.GetTextLen). Each line is terminated with CRLF (#13#10).

Is this a bug? How can I reliably check if everything is selected in the TRichEdit?

I use Delphi 10.4, if it changes anything.

CodePudding user response:

As mentioned in this topic, RichEdit 2.0 replaces CRLF pairs with CR internally, and retrieves LF's in some cases.

As workaround - calculate number of lines in selected range to make correction (SelText contains only CR's, GetTextLen works with text with retrieved CRLF, so counts both CR's and LF's). Remy Lebeau proposal is used.

var
  sel, getl, crcnt, i: integer;
  tx: string;
begin
  sel := RichEdit1.SelLength;
  getl := RichEdit1.GetTextLen;
  crcnt := SendMessage(Richedit1.Handle, EM_EXLINEFROMCHAR, 0, sel);
  Memo1.Lines.Add(Format('%d %d',[sel, getl - crcnt   1]));
end;
  • Related