TListView
│
English (en) │
français (fr) │
polski (pl) │
русский (ru) │
TListView is a visible component on the Common Controls tab of the Component Palette that provides a list view of its associated Items. Items can be represented by an icon from each of the associated TImageLists: SmallImages, LargeImages and StateImages.
Notes
- GetNextItem() requires StartItem to be provided. This is incompatible with Delphi's GetNextItem(), where startItem is allowed to be nil, which will perform the search from the relative (direction driven) start.
Sorting
TODO: add description on how OwnerData affects the sorting
Sorting is (typically?) applied when using vsReport ViewStyle, as it's column driven (with Sortcolumn index required). Sorting might be a time consuming for a larger data scale (when comparison of items takes a considerable time).
SortType is the key property in order to perform any sorting in TListView.
- stNone - (the default value) that no sorting should be for the control.
- stText - the text of a cells would be used for item comparison. The actual text entry is controlled by SortColumn property. Note the default sorting comparison is actually treats the text as Ansi, rather than UTF8. Thus the result might be undesired for the non-English texts
- stData - the default sorting comparison treats Data property as an integer value (by casting pointer to an integer) and sorts the according to value. (in most cases such sorting is useless, unless "Data" is an actual integer casted to the pointer)
- stBoth - treated as stText for in the default sorting comparison.
Unless using text sorting, the default comparison method is not the best approach, instead a custom comparison could be used. The custom comparison is assigned with OnCompare event of TListView. The following parameters are passed:
- Sender - TListview that's being sorted
- Item1 - one TListItem being compared
- Item2 - another TListItem being compared to the one
- Data - is not used and is always passed as zero. (provided for Delphi compatibility)
- Compare - the result of the comparison, that must be populated by the event. The following values should be assigned to the Compare argument.
- -1 - if Item1 should show up "earlier" in Ascending order, than Item2. (Item1 is less than Item2)
- 0 - if two items are equal
- 1 - if Item2 should show up "earlier" in Ascending order, than Item1. (Item2 is less than Item1)
- It's a normal practice to reverse the comparison result value, if SortDirection is descending. I.e.:
procedure TForm1.ListView1OnCompare(Sender: TObject; Item1, Item2: TListItem;
Data: Integer; var Compare: Integer)
begin
... do the comparison...
if TListView(Sender).SortDirection = sdDescending then
Compare := -Compare;
end;
The OnCompare event should check for SortType, SortColumn and SortDirection properties of TListView, and do adjust the comparison result respectively of those properties.
Method Sort performs of items, if SortType is other than stNone and SortColumn is assigned to a valid column index.
Method CustomSort performs a one-time sorting using a provided sorting function.
- ASortProc: TLVCompare - sorting function
- AOptionalParam: PtrInt - sorting parameter
Sorting parameter is ignored (provided for the Delphi compatibility). If ASortProc is passed as nil, then the either sorting OnCompare is used or default sorting.
Typically the sorting can be requested by a user. Whenever a user clicks on a column header, that requires to either sort (initially ascendancy) or to switch to the opposite sorting order (either from ascendant to descendant, or back). It's not a rule, but a common expectation. For some data sets selecting sorting might not make any sense. AutoSort (default value is true)the property of TListview controls, if the listview should be sorted automatically, as soon as user clicks on a column header. AutoSort will have no effect, while SortType is set to stNone.
AlphaSort is a convenience method, that changes the settings to ascending sort (SortDirection=sdAscending) based on the text (SortType=stText) of the first column (SortColumn=0). Note that the actual properties are changed.
Sort Indicator
Originating from: https://bugs.freepascal.org/view.php?id=36281
In trunk since r62567
1) Add a new property SortIndicator to TListViewColumn. The property controls system native sort indicator.
The indicator is acting solely as a decorative feature. (Doesn't have any effect on the sorting). The indicator has 3 possible values
- siNone - no sort indicator should be shown
- siAscending - ascending indicator should be shown
- siDescending - descending indicator should be shown
The property can work independently of other TListview sorting properties.
2) Add new property AutoSortIndicator to TListView. The property is intended to be used as a compliment to AutoSort property. If both are enabled, LCL code controls the current selection of the column sortindicator according to AutoSort rules.
Early 2021 it was noticed that if you are using Qt5 and you have TListView.SortDirection set to sdAscending (the default), then setting a column's Sort Indicator to siDescending displays an ascending indicator. This does not happen with GTK2 or GTK3. Setting SortDirection to sdDescending makes all other combinations work fine so, seems an easy solution. Logged https://bugs.freepascal.org/view.php?id=38393
Custom drawing
There is a good article on custom-drawing TListView: https://delphidabbler.com/articles/article-16. Most of the sample code in this article is working in Lazarus, too, however only on Windows. Here are some excerpts:
Note: Keep the OwnerDraw property of the TListView at false.
Alternating row colors
Attach this handler for the OnCustomDrawItem event which is responsible for painting a full row. When the DefaultDraw parameter of the event handler is unchanged (i.e., true) then control still paints the text and additional gimmicks such as icons or checkboxes (see screenshot at the right):
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
if Odd(Item.Index) then
// odd list items have green background
ListView1.Canvas.Brush.Color := clMoneyGreen
else
// even list items have window colour background
ListView1.Canvas.Brush.Color := clWindow;
end;
Individual column colors
At first prepare a procedure to set the brush color for each ListView column:
{ Sets the brush colour for the column. Our ListView has 3 columns }
procedure TForm1.SetLVColumnColour(AColIndex: Integer);
const
cRainbow: array[0..2] of TColor = ($FFCCCC, $CCFFCC, $CCCCFF);
begin
ListView1.Canvas.Brush.Color := cRainBow[AColumnIndex];
end;
Then provide a handler of the OnCustomDrawItem event which is responsible for painting of the entire row and, in particular, of the 1st column, and the OnCustomDrawSubItem event which is called when the other columns are painted:
{ Draw the "Caption" column }
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
SetLVColumnColour(0);
end;
{ Draw the sub item columns }
procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
Item: TListItem; SubItem: Integer; State: TCustomDrawState;
var DefaultDraw: Boolean);
begin
SetLVColumnColour(SubItem);
end;
Widgetset Implementation
ListView is implemented via TWSCustomListViewClassinterface.
Method | Description |
---|---|
SelectAll | The method provides a parameter AIsSet.
If the parameter is set to true then all items should selected in the view. If it's false then, all parameters should deselected. It's important that the event shouldn't trigger any events, as LCL will call SelectChange event by itself. |
See also