Board index » delphi » Searching a ListView

Searching a ListView

Quote
>>I have a TListView-Component in vsReport-Style built into my
>>application. Now I want to search within the listed data. When I do
that
>>using the FindCaption()-Procedure the program only searches in the
first
>>column. How can I search in the rest of the columns? Is there another
>>Procedure or am I just doing something wrong?
>As far as I know you will have to write your own loop to look through
the
>subitems.

Well, the problem is, that I can't tell the procedure in which
(sub)-item it shall look... That's the problem...

Thanks anyway...

 

Re:Searching a ListView


Quote
>Well, the problem is, that I can't tell the procedure in which
>(sub)-item it shall look... That's the problem...

Are just looking for a string, and want to know in which Item it is
contained?

Something like this?

Function lvFindText(lv:TListView;Str:String):TListItem;
Var i,ii : Integer ;
Begin
   // Is it in the Caption?
   Result := ListView1.FindCaption( 0,Str,false,true,false) ;

   If Result = Nil Then Begin
      // If not in the caption, search SubItems
      For i := 0 To (lv.Items.Count-1) Do Begin
         For ii := 0 To (lv.Items.Item[i].SubItems.Count-1) Do Begin
            If (Str = lv.Items.Item[i].SubItems[ii]) Then Begin
               Result := lv.Items.Item[i] ;
               Break ;
            End;
         End;
      End;
   End;
End;

Var li : TListItem ;

li := lvFindText(ListView1,'My ListView Text');
If li<>Nil Then Begin
   ShowMessage('Row Caption:'+li.Caption+#13+'Row
Index:'+IntToStr(li.Index));
   li.Selected := True ;
End Else
   ShowMessage('Text Not Found');

-Steve-

Re:Searching a ListView


Quote
>Are just looking for a string, and want to know in which Item it is
>contained?

Yeah, that's exactly what I want to do! Thanks for the function! Now,
there are two things about it that I don't understand:

- I have three entries with the caption 'Test' in three different
subitems. The function always reports the third entry. As I want to
include a "search again"-button in my program that searches the ListView
for more entries with that caption, this sucks... ;-)

- Though I've set the "Partial, Inclusive, Wrap"-parameters of
FindCaption() to true the function does not search again from the top if
it can't find anymore entries below.

Thanks in advance!

 Patrik

Quote
>-Steve-
Steve Zimmelman wrote:
> >Well, the problem is, that I can't tell the procedure in which
> >(sub)-item it shall look... That's the problem...

> Are just looking for a string, and want to know in which Item it is
> contained?

> Something like this?

> Function lvFindText(lv:TListView;Str:String):TListItem;
> Var i,ii : Integer ;
> Begin
>    // Is it in the Caption?
>    Result := ListView1.FindCaption( 0,Str,false,true,false) ;

>    If Result = Nil Then Begin
>       // If not in the caption, search SubItems
>       For i := 0 To (lv.Items.Count-1) Do Begin
>          For ii := 0 To (lv.Items.Item[i].SubItems.Count-1) Do Begin
>             If (Str = lv.Items.Item[i].SubItems[ii]) Then Begin
>                Result := lv.Items.Item[i] ;
>                Break ;
>             End;
>          End;
>       End;
>    End;
> End;

> Var li : TListItem ;

> li := lvFindText(ListView1,'My ListView Text');
> If li<>Nil Then Begin
>    ShowMessage('Row Caption:'+li.Caption+#13+'Row
> Index:'+IntToStr(li.Index));
>    li.Selected := True ;
> End Else
>    ShowMessage('Text Not Found');

> -Steve-

Re:Searching a ListView


Well, in that case you might not want to use the FindCaption function and
just resort to searching through the items in the For loop.

Function lvFindText(lv:TListView;Str:String):TListItem;
Var i,ii : Integer ;
Begin
   // Is it in the Caption?
   // Result := ListView1.FindCaption( 0,Str,false,true,false) ;

   // -- If Result = Nil Then Begin
      // If not in the caption, search SubItems
      For i := 0 To (lv.Items.Count-1) Do Begin
         If Not (lv.Items.Item[i].Caption = Str) Then Begin
            For ii := 0 To (lv.Items.Item[i].SubItems.Count-1) Do Begin
               If (Str = lv.Items.Item[i].SubItems[ii]) Then Begin
                  Result := lv.Items.Item[i] ;
                  Break ;
               End;
            End;
         End Else Result := lv.Items.Item[i] ;
      End;
   // -- End;
End;

You'll have to code your own "Search Again" implementation and of course
store the Index of the last item found; and when you fire the SearchAgain,
start with the last Index.  So you could probably add a Parameter of Index
in the lvFindText function and use that parameter in the outside "For" loop.
Instead of using For I := 0 To ... you can use For I := MyIndex To ....

-Steve-

Other Threads