Author: Tomas Rutkauskas
How to search for text in a TListView item
Answer:
Parameters:
lv:
The listview, supposed to be in vaReport mode
S:
The text to search for
Column:
The column index for the column to search , 0-based. Returns the found listview
item,
or Nil if none was found
Precondition:
lv <> nil, lv in report mode if column > 0, S not empty
Description:
The search is case-insensitive and will only match on the complete column content.
Use
AnsiContainsText instead of AnsiCompareText to match on a substring in the columns
content.
1 2 function FindListviewItem(lv: TListview; const S: string; column: Integer):
3 TListItem;
4 var5 i: Integer;
6 found: Boolean;
7 begin8 Assert(Assigned(lv));
9 Assert((lv.viewstyle = vaReport) or (column = 0));
10 Assert(S <> '');
11 for i := 0 to lv.items.count - 1 do12 begin13 result := lv.Items[i];
14 if column = 0 then15 found := AnsiCompareText(result.caption, S) = 0
16 elseif column <= result.subitems.count then17 found := AnsiCompareText(result.subitems[column - 1], S) = 0
18 else19 found := false;
20 if found then21 Exit;
22 end;
23 {No hit if we get here}24 Result := nil;
25 end;