44 Votes

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

Tip by Delphian | 2013-10-05 at 12:37

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);
var
  i: integer;
begin
  if (Shift = [ssCtrl]) and (key = ord('A')) then begin
     ListView1.Items.BeginUpdate;
     for i := 0 to ListView1.Items.Count-1 do Items[i].Selected:=true;
     ListView1.Items.EndUpdate;
  end;
end;

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.

ReplyPositiveNegative

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.

Participate

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