Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searxh for string in listview 3

Status
Not open for further replies.

simmo09

Programmer
Apr 22, 2009
82
GB
Hi, i have a listview in vsReport, i found this code on torry:

Code:
// Function to search items and select if found
// Funktion, um Items zu suchen unda falls gefunden, sie danach markieren

procedure LV_FindAndSelectItems(lv: TListView; const S: string; column: Integer);
var
  i: Integer;
  found: Boolean;
  lvItem: TListItem;
begin
  Assert(Assigned(lv));
  Assert((lv.ViewStyle = vsReport) or (column = 0));
  Assert(S <> '');
  for i := 0 to lv.Items.Count - 1 do
  begin
    lvItem := lv.Items[i];
    if column = 0 then
      found := AnsiCompareText(lvItem.Caption, S) = 0
    else if column > 0 then
    begin
      if lvItem.SubItems.Count >= Column then
        found := AnsiCompareText(lvItem.SubItems[column - 1], S) = 0
      else 
        found := False;
    end
    else
      found := False;
    if found then
    begin
      lv.Selected := lvItem;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  lvItem: TListItem;
begin
  // in der Spalte subitem[0] den Text aus Edit1 suchen
  LV_FindAndSelectItems(ListView1, Edit1.Text, 1);
  ListView1.SetFocus;
end;

It works very good, the only problem is you need to type an exact match string.

so i need to know:

1) how can i search and find any part of the string?
for example if in column 2 i have this subitem: "Hello Tek Tips", if i search "llo" it should pick up the subitem.

2) if there is better code please post it.

Thanks!
 
Replace:
Code:
 found := AnsiCompareText(lvItem.Caption, S) = 0
with:
Code:
 found:= pos(S, lvItem.Caption, S) > 0

AND replace:
Code:
found := AnsiCompareText(lvItem.SubItems[column - 1], S) = 0
with:
Code:
found:= pos(S, lvItem.SubItems[column - 1]) > 0

un-tested.

Roo
Delphi Rules!
 
if you want case-insensitive, partial search, you can just replace the AnsiCompareText functions with AnsiContainsText function. Look in delphi help for the exact syntax.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
@daddy - I've been 'rolling-my-own' so long I neven think to buy a pack. LOL Every time I look at SysUtils routines in help, I'm amazed to wonder; "How long's THAT been in there?"
*4u

Roo
Delphi Rules!
 
Roo's seems to work pretty ok, thank you.

thanks aswell whosrdaddy, forgot about checking SysUtils :)
 
forgot to add, you can use the SameText function for matching any case
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top