Selecting a line with the following code in the OnKeyUp event of a Memo drifts the selection past the beginning of each line > 0. So I adjusted the line break offset to -2 and the line length offset to -1. First I thought this was a Linux problem due to line ending differences with Windows (#10 vs #13#10), but the code seems to fix the same problem on both Linux (Debian 12/GTK2+) and Windows (7).
// Get current line number
Line := Memo.CaretPos.Y;
// prevent selection drift (both Windows and Linux)
LineBreakOffset := Length(sLineBreak) - 2;
LineLengthOffset := -1;
// Calculate the starting character position of the line
LineStart := 0;
for I := 0 to Line - 1 do
LineStart := LineStart + Length(Memo.Lines[I]) + LineBreakOffset;
// Get the length of the current line
LineLength := Length(Memo.Lines[Line]) + LineLengthOffset;
if LineLength < 0 then
LineLength := 0; // Prevent negative length
// Select the line
Memo.SelStart := LineStart;
Memo.SelLength := LineLength;
Memo.SetFocus;
This seems more like an LCL issue.