Code Modification Help Needed

Hi all,

I need some help modifying some code.
The code will basically searches and displays contents on a ListView.
This code works but it only displays the results of the search.

What I need it to do is the display the results of the test (Like it does already) and under that, the rest of the data (Not just the results).

So, if the ListView had 10 rows of data...and the search result was 2 found it will display these results First (It already does that)...and also the 8 rows remaining under that.

Here is the code:

procedure TForm1.ComboBox1Change(Sender: TObject);

var
  i1, i2: integer;
  s1: string;
  b1: boolean;
begin

  s1:= ComboBox1.text;  // our search-word

  listview1.items.beginupdate;
  for i1:= listview1.items.count- 1 downto 0 do begin
    b1:= pos(s1, listview1.items[i1].caption)> 0;
    if not b1 then for i2:= 0 to listview1.items[i1].subitems.count- 1 do begin
      if pos(s1, listview1.items[i1].subitems[i2])> 0 then begin
        b1:= true;
        break;
      end;
    end;
    if not b1 then listview1.items.delete(i1);
  end;
  listview1.items.endupdate;

end;

Hope you can help!

Thanks

ST3VO


ST3VOAsked:
Who is Participating?
 
MerijnBSr. Software EngineerCommented:
after some more thinking I found that the easiest way is to use the listviews own sort method.

In the combobox change event only do:

procedure TForm14.ComboBox1Change(Sender: TObject);
begin
 ListView1.CustomSort(nil, 0);
end;

then make a ListView.OnCompare event, like this:

procedure TForm14.ListView1Compare(Sender: TObject; Item1, Item2: TListItem; Data: Integer; var Compare: Integer);

 function IsHit(ListItem: TListItem; SearchText: string): boolean;
 var i: integer;
 begin
  result := false;

  if Pos(SearchText, ListItem.Caption) > 0 then
  begin
   result := true;
   exit;
  end;

  for i := 0 to ListItem.SubItems.Count - 1 do
   if Pos(SearchText, ListItem.SubItems[i]) > 0 then
   begin
    result := true;
    exit;
   end;
 end;

var Hit1, Hit2: boolean;
begin
 Hit1 := IsHit(Item1, ComboBox1.Text);
 Hit2 := IsHit(Item2, ComboBox1.Text);

 if Hit1 = Hit2 then Compare := 0 else
 if Hit1 and (not Hit2) then Compare := -1 else
 Compare := 1;
end;
0
 
rfwoolfCommented:
I'll have to write up a solution later, but I'll tell you what I can see so far:
In your listview you are deleting all the negative results

Perhaps you should have two listviews: Listview1 and Listview2
Go through Listview1 and if a search is positive, add it to the TOP of Listview 2
If a search is negative, add it to the BOTTOM of Listview 2.
Then display Listview 2.
0
 
ST3VOAuthor Commented:
Would using 2 components slow down the process?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MerijnBSr. Software EngineerCommented:
not really, how many items are there approx. in the list?
0
 
ST3VOAuthor Commented:
This will change ...could be 50 or 30000

I've got an idea...

This line deletes the negative data.

How could I change:

  if not b1 then listview1.items.delete(i1);

So, it will append the data on listview1 after the search?

In other words:

1. Display the result (As it's already doing)
2. Instead of deleting the results: if not b1 then listview1.items.delete(i1);

Add them to the end of the list.

Can this be done without using 2 listview's?

0
 
MerijnBSr. Software EngineerCommented:
that is what rfwoolf is suggesting, it works, but you're 'non hits' will have reversed order.
If this is a problem best is to temporary place the 'non hits' in a container and add them to the list afterwards.
0
 
ST3VOAuthor Commented:
Would it be possible to help some example code please?

0
 
ST3VOAuthor Commented:
That was spot on!!!

Perfect!!!

Thanks a million :o)

ST3VO
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.