I am doing a search in a Tmemo with the following code
function TForm_Test.FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;
begin
Result := PosEx(AString, AMemo.Text, StartPos);
if Result > 0 then begin
AMemo.SelStart := UTF8Length(PChar(AMemo.Text), Result - 1);
AMemo.SelLength := Length(AString);
AMemo.SetFocus;
end; // if
end; // FindInMemo
This gives me a number which is from the start of the Tmemo.
FindInMemo has a little bug:
AMemo.SelLength := UTF8Length(AString);
But how can I determine the linenumber from this?
I found this but this is for Delphi and not Lazarus.
mi := Memo_Test.Perform(EM_LINEFROMCHAR, i, 0);
For Windows, use SendMessage:
mi := SendMessage(Memo_Test.Handle, EM_LINEFROMCHAR, i, 0);
i has to be for UTF16, but if you pass
-1 instead of
i, you get the line that has the caret/selection, without caring about UTF16.