Example: Using cxListView in virtual mode
- 3 minutes to read
This example explains using cxListView control in virtual mode. This mode is activated when the OwnerData property of a list view control is set to True. In this case, a user must manage the OnData, OnDataFind, OnDataHint and OnDataStateChange event handlers.
Event | Description |
---|---|
On |
Fires before displaying of an item within a virtual list view. This event also fires when list view items need to be updated. |
On |
Fires when the list view control receives a search request, such as when the Find |
On |
Fires before the On |
On |
Fires when the item’s state changes. |
When the list view control functions in virtual mode, it contains no information about items within it, except for their number and selection/focus position. The number of items within the virtual list view must be specified by the Items.Count property value.
Before the virtual list view is loaded, the OnData event is fired as many times as specified by the Items.Count property. Handle this event to supply the necessary item properties: Caption, ImageIndex, StateIndex, SubItems, etc.
Note
if the Items.
The following code demonstrates the OnData and OnDataFind event handlers. The OnData event handler populates the list view with items. Item and sub-item captions are obtained from the strings array. The OnDataFind event handler implements incremental search functionality during user typing within the list view control.
procedure TForm1.FormCreate(Sender: TObject);
begin
cxListView1.Items.Count := 10;
end;
procedure TForm1.cxListViewData(Sender: TObject; Item: TListItem);
const
Names: array[0..10, 0..1] of string = (
('Davolio', 'Nancey'), ('Fuller', 'Andrew'), ('Leverling', 'Janet'),
('Peacock', 'Margaret'), ('Buchanan', 'Steven'), ('Suyama', 'Michael'),
('King', 'Robert'), ('Callahan', 'Laura'), ('Dodsworth', 'Anne'),
('Diego', 'Roel'), ('Paula', 'Wilson'));
begin
Item.Caption := Names[Item.Index][0];
Item.ImageIndex := Item.Index;
Item.StateIndex := 1;
Item.SubItems.Add(Names[Item.Index][1]);
Item.SubItemImages[0] := High(Names) - Item.Index;
end;
procedure TForm1.cxListViewDataFind(Sender: TObject; Find: TItemFind;
const FindString: String; const FindPosition: TPoint; FindData: Pointer;
StartIndex: Integer; Direction: TSearchDirection; Wrap: Boolean;
var Index: Integer);
var
I: Integer;
Found: Boolean;
begin
I := StartIndex;
//if the search is performed during user typing (i.e. item is located according
//to its Caption)
if (Find = ifPartialString) or (Find = ifExactString) then
begin
repeat
//if the search has reached the end of the Items collection
if I = cxListView1.Items.Count - 1 then
//wrapping the search direction
if Wrap then I := 0 else Exit;
//if the current item caption contains the typed character, the Found flag is
//activated. This flag indicates that the search was successful
Found := Pos(UpperCase(FindString), UpperCase(cxListView1.Items[I].Caption)) = 1;
//moving to the next item
Inc(I);
//until the search was completed successfully or the all items were involved
//into search
until Found or (I = StartIndex);
end;
//if the search was completed successfully, the found item is selected
if Found then Index := I - 1;
end;