Could you use Richmemo? Here's what I am thinking to color specific lines:
Use the
TRichMemo.SelStart and
TRichMemo.SelLength properties to select the specific line.
Set the
TRichMemo.SelAttributes.Color property to change the color of the selected text.
Create a
procedure to color a line and apply it in the
onChange event.
Here's an example:
uses
StrUtils // ...and your other units
procedure ColorLine(RichMemo: TRichMemo; LineIndex: Integer; Color: TColor);
var
StartPos, EndPos, LineCount: Integer;
Text: string;
begin
LineCount := RichMemo.Lines.Count;
// Check if the requested line exists
if (LineIndex < 0) or (LineIndex >= LineCount) then
Exit;
// Get the full text
Text := RichMemo.Text;
// Find the start position of the line
StartPos := 1;
for var i := 0 to LineIndex - 1 do
StartPos := PosEx(LineEnding, Text, StartPos) + Length(LineEnding);
// Find the end position of the line
if LineIndex = LineCount - 1 then
EndPos := Length(Text) + 1
else
EndPos := PosEx(LineEnding, Text, StartPos);
// Select the line
RichMemo.SelStart := StartPos - 1; // Adjusting for 0-based index
RichMemo.SelLength := EndPos - StartPos;
// Set the color
RichMemo.SelAttributes.Color := Color;
// Clear the selection
RichMemo.SelLength := 0;
end;
You can then use this procedure to color a specific line in the
onChange event:
ColorLine(RichMemo1, 8, clRed); // Colors the 9th line (index 8) red
This should allow you to color entire lines. If you need more granular control (e.g., coloring parts of lines), you would need to adjust the selection range.