Search code examples
delphi-7

Highlight word in TListView Delphi 7


I'm trying to highlight a word from TListView but I can't make it work. My first attempt is to highlight the first letter of each row but won't work. TListView won't display anything. Here is my code:

procedure TfrmMain.lvMainDrawItem(Sender: TCustomListView; Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
  r : TRect;
  c : TCanvas;
begin
  r := Item.DisplayRect(drBounds);
  c := Sender.Canvas;

  c.Brush.Color := clWindow;
  c.Font.Color := clWindowText;
  c.TextRect(r, 1, 1, 'a');
end;

I based my code here but it is written in XE10.4. I'm using delphi 7. How can I highlight word/first letter of each row in TListView?


Solution

  • You are drawing outside of the item's bounding rectangle, that is why you don't see anything. The X,Y coordinates you specify to TextRect() are relative to the top/left corner of the ListView's client area, but are clipped by the specified TRect.

    Try this instead:

    c.TextRect(r, r.Left+1, r.Top+1, 'a');