55 Votes

Delphi/Lazarus: Select all with CTRL+A in ListView

Tip by Delphian | Last update on 2021-06-28 | Created on 2013-10-05

If you have written an application in Delphi or Lazarus containing a TListView with some entries, it is quite nice to have the possibility to select all items of the ListView by pressing the key shortcut CTRL and A on the keyboard. Unfortunately, the ListView does not support this automatically.

But we can quickly remedy this by writing the following code in the OnKeyDown event of our ListView:

procedure Tprog.ListView1KeyDown(Sender: TObject; var Key: Word; 
Shift: TShiftState);
  i: integer;
  if (Shift = [ssCtrl]) and (key = ord('A')) then begin
     for i := 0 to ListView1.Items.Count-1 do Items[i].Selected:=true;

First of all, this code checks which keys have been pressed. If the keys are "CTRL" (Shift = [ssCtrl]) and "A", we go through all entries of the ListView and set "Selected" to "true". Of course, you can still add conditions for other keys or key combinations.

Important: To be able to select multiple items in the ListView at all, we have to set the property "MultiSelect" of our ListView to "true" before.


About the Author

AvatarThe author has not added a profile short description yet.
Show Profile


Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.