Lazarus RichMemo SetRangeColor not working as I expect

54 views Asked by At

I have a list of strings I want to compare character-by-character with a base string, and to accentuate their congruencies in a RichMemo component by changing the font colour of the of the particular character in each line of the RichMemo.

I have form with a button and a RichMemo component, referred to in the following code.

procedure TForm1.Button1Click(Sender: TObject);

var
  I, J, start: Integer;
  weasel:string;

begin
 weasel := 'methinks it is like a weasel';
 RichMemo1.Clear;
 RichMemo1.Font.Name := 'Consolas';       // Monospace font
 RichMemo1.Font.Size := 16;               // Embiggen the size for older eyes.
 RichMemo1.Lines.CommaText:=
   '"mrthinkseit is liue arwmasel",' +
   '"mrthinkseit is liue arwmasel",' +
   '"mrthinkseit is liue arwmasel",' +
   '"mrthinkseit is liue arwmasel",' +
   '"mrthinkseit is liue arwoasel",' +
   '"mrthinkseit is like arwoasel",' +
   '"mrthinkseit is like arwoasel",' +
   '"mrthinkseit is like arwoasel",' +
   '"mrthinkseit is like a woasel",' +
   '"mrthinkseit is like a woasel",' +
   '"mrthinkseit is like a woasel",' +
   '"mrthinkseit is like a woasel",' +
   '"mrthinkseit is like a woasel",' +
   '"mrthinks it is like a woasel",' +
   '"methinks it is like a weasel"';

   for I := 0 to RichMemo1.Lines.Count - 1 do begin
      for J := 1 to 28 do begin
          if  RichMemo1.Lines[i][j] = weasel[j] then  begin
              start := I * 28 + j;
              RichMemo1.SetRangeColor(start, 1, clRed);
          end;
      end;
   end;
end;
end.     

Unfortunately there are 'wrong' letters getting highlighted in red, as well as some 'right' letters not getting highlighted at all.

I have tried running the for-loops zero-based, as well as trying to account for perhaps hidden return codes in the RichMemo but I cannot see a pattern.

0

There are 0 answers