[Lazarus] TListView: Column Header Captions and Column Sorting
Michael Van Canneyt
michael at freepascal.org
Thu Nov 17 16:35:48 CET 2022
On Thu, 17 Nov 2022, Aruna Hewapathirane wrote:
> <snip>
>
> Hello again,
>
> I have column headers and sorting works on a button click . Thank you once
> again Michael.
>
> I am trying to figure out how to sort when a user clicks a column header?
>
> Screenshot: https://pasteboard.co/QSf8hNafG0x5.png
>
> And I notice the column headers are sort of greyed out. Is it possible to
> make them bold if we want?
> And the column width am guessing we can set through code?
In the IDE, set a OnColumnClick event handler. Define FLastColumnIndex and
FLastSort as form fields, so your form will look like this:
TForm1 = Class(TForm)
ListView1 : TListView;
procedure ListView1ColumnClick(Sender: TObject; Column: TListColumn);
private
FLastColumnIndex : Integer;
FLastSort : TSortDirection;
end;
You can also set the event handler in code:
ListView1.OnColumnClick:=@ListView1ColumnClick;
In the OnColumnClick handler, do the following:
procedure TForm1.ListView1ColumnClick(Sender: TObject; Column: TListColumn);
var
SD : TSortDirection;
begin
SD:=sdAscending;
if Column.Index=FLastColumnIndex then
if FLastSort=sdAscending then
SD:=sdDescending;
ListView1.SortColumn:=Column.Index;
ListView1.SortDirection:=SD;
ListView1.Sort;
end;
That should sort ascending if the user clicks on the column for the first
time, and will toggle between ascending/descending if the user clicks
on the same column.
Michael.
More information about the lazarus
mailing list