views:

67

answers:

2

Hi,

When you set the Caption of a TListItem it seems to always set the Text for the first column in the row. When you start typing in the ListView it will search & select the closest match based on the caption of the first column.

I have a situation where I need the caption of the first row to be empty, but still need the search functionality to work as normal (in this case the data I would be searching for may be in the 2nd/3rd column).

Is this possible without using any 3rd party controls?

A: 

If it's bound to a dataset, then you can do your own search and then move the dataset cursor to the row that you want. Just off the top of my head, because I just did one of those.

Update: Use the OnCompare handler, and do your own comparision on whatever criteria you want. i.e. you get to decide whether item1 < item2 or not.

Here's a nice writeup: http://www.latiumsoftware.com/en/delphi/00011.php

Chris Thornton
@Chris: Not its not bound to a dataset. It is populated at runtime.
James
@Chris: The `OnCompare` method is for sorting the ListView, don't think this will help me for when I search it.
James
@Chris: Tested the 'OnCompare' defintely only for sorting the items as they are added no influence on the searching. If the first column does not have a caption then the search doesn't seem to work.
James
Right - sorry. I confused search/sort in my head. I think you need OnDataFind.
Chris Thornton
@Chris: The `OnDataFind` doesn't seem to get triggered when the Caption is not set. I think what I need can't be done natively :(
James
Hmm.. You an always resort to keypress events.
Chris Thornton
@Chris: I was hoping I didn't have to. The TListView has a `FindCaption` method which is non-overridable....shame!
James
+1  A: 

Depending on why you want the caption/ first column to be blank, you could move the text you want to search for into the caption, and then have a blank sub-item. Then swap the column order in code like so

//Move the 1st sub-item left one column
ListView1.Columns[1].Index := 0;

This would look almost the same, with the exception that if you don't have RowSelect set to true the highlighted caption will be in the wrong column. This would allow you to search as required and use the FindCaption method in code.

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  li : TListItem;
begin
    //Add data to the list view for demo
    for I := 0 to 10 do
    begin
           li := ListView1.Items.Add;    
           li.Caption := intToStr(Random(10000));
           li.SubItems.Add('');
           li.SubItems.Add('Col2');

           //addimages so you can see which column is which
           li.SubItemImages[0] := 0;
           li.ImageIndex := -1;
    end;

    //move column 2 left one, this is the important bit
    ListView1.Columns[1].Index := 0;
end;

alt text

Re0sless