[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

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


0
ST3VO
Asked:
ST3VO
  • 4
  • 3
1 Solution
 
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
 
MerijnBSr. Software EngineerCommented:
not really, how many items are there approx. in the list?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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
 
ST3VOAuthor Commented:
That was spot on!!!

Perfect!!!

Thanks a million :o)

ST3VO
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now