Link to home
Create AccountLog in
Avatar of ST3VO
ST3VOFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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


Avatar of rfwoolf
rfwoolf
Flag of South Africa image

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.
Avatar of ST3VO

ASKER

Would using 2 components slow down the process?
Avatar of MerijnB
not really, how many items are there approx. in the list?
Avatar of ST3VO

ASKER

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?

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.
Avatar of ST3VO

ASKER

Would it be possible to help some example code please?

ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of ST3VO

ASKER

That was spot on!!!

Perfect!!!

Thanks a million :o)

ST3VO