Is there some other method to work it out?
...
eg can the SelStart property be used as startin point, or some other function which computes the character position?
In GTK it does work (internally gtk_text_iter_get_line is used for a Memo).
You could use the SelStart and count the number of #13's (from 0 to SelStart). It's a crude method and only works for small texts but should work.
For instance, you could do this:
function OccurrencesOfChar(const S: string; const C: char): integer;
var
i: Integer;
begin
result := 0;
for i := 1 to Length(S) do
if S[i] = C then
inc(result);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Line: Integer;
begin
Line := OccurrencesOfChar(Copy(Memo1.Text, 1, Memo1.SelStart), #13);
Showmessage('Line: ' + IntToStr(Line) + ' = ' + Memo1.Lines[Line]);
end;
(Note: Line is 0 based)
Edit: Woops. I'm not sure how you would like the count to be with line-wrapping? Above method doesn't take into account the line could be wrapped.
Do you need the real line-number or just the string-text of the line where your caret is at?
If that's the case you could do something like this:
function GetCurrentLineString(Memo: TMemo): String;
var
bChar, eChar: Integer;
begin
bChar := Memo.SelStart;
while (bChar > 0) and (Copy(Memo.Text, bChar - 1, 1) <> #13) do Dec(bChar);
eChar := bChar;
while (eChar < Length(Memo.Text)) and (Copy(Memo.Text, eChar + 1, 1) <> #13) do Inc(eChar);
Result := Copy(Memo.Text, bChar, eChar - bChar + 1);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage('Line: ' + GetCurrentLineString(Memo1));
end;
(Note: A line does include the entire line, even if it's wrapped)