Hi all,
I'm using Lazarus 2.0.12 on Windows 10.
I have a TMemo on a form, and want to track the caret position in the format row nn : col nn.
I'm handling OnChnage, OnKeyDown, OnKeyUp and also mouse events, calling a procedure that updates a TStatusBar with the Memo's caret position from each event handler:
StatusBar1.Panels[0].Text:= 'Row '+ IntToStr(TheMemo.CaretPos.y+1) + ' : Col '+IntToStr(TheMemo.caretPos.x+1);
This works, in that moving the caret around with the cursor keys, typing text, or clicking the mouse gives the correct caret position. However, things go a bit awry when I hold down shift to select some text. Suppose I have the following two lines of text in the Memo:
abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz
If I move the caret to the middle of a line, hold down shift and move the cursor to the right, the caret position reflects the end of the selection. If I hold down shift and move to the left, the caret position doesn't change, even though there is a blinking caret at the left end of the selection. If I position the caret in the second line (say, immediately before the letter p, then hold down shift and move up a line, such that the selection starts with "p" in the first line and ends with "o" in the second line, the caret position is reported as Row 2 : Col 44.
I'm guessing I could probably work around that somehow with SelStart and SelLength, but SelStart returns a character index into the text of the Memo. How can I convert that back to a line number? I don't see any equivalent of EM_LINEFROMCHAR?
I'm probably overlooking something really obvious here, but any suggestions would be very much appreciated.
TIA
Rob.