55 Votes

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

Tip by Stefan Trost | Last update on 2022-11-18 | 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, a ListView does not support this function automatically.

But we can quickly remedy this by writing the following code into 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 
        ListView1.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] - the Shift-Key is pressed) and "A" (key = ord('A') or also key = 65), we go through all entries of the ListView and set the property "Selected" of each item to "true". So that the ListView is not drawn again after each of these individual changes, we call .BeginUpdate and .EndUpdate before and after looping through the elements. 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 (for example via the Object Inspector). Otherwise, this property is disabled by default so that only one element of the Listview can be selected at the same time.

More General Solution

In our first code example, we directly work with a concrete ListView that bears the name ListView1. You can do it in this way, but in some cases a more general solution is more elegant. For example, if we have several ListViews and each of them should react to our key combination in the same way. In this case, we can write a general procedure that we can then assign to each ListView without having to write the same code for multiple times.

In the course of this, we take advantage of the variable "Sender" that contains a reference to the calling ListView. This is how we know which ListView called up the code so that we can continue to work with this ListView. A general code could look like this:

procedure Tprog.ListViewKeyDown(Sender: TObject; var Key: Word; 
  Shift: TShiftState);
var
  AListView: TListView;
  i: integer;
begin
  if not (Sender is TListView) then exit;
  AListView := (Sender as TListView);

  if (Shift = [ssCtrl]) and (key = 65) then begin
     AListView.Items.BeginUpdate;
     for i := 0 to AListView.Items.Count - 1 do
         AListView.Items[i].Selected := true;
     AListView.Items.EndUpdate;
  end;
end;

Not much has changed compared to the first code. Instead of accessing "ListView1" directly, this time we work with a variable that we have called "AListView" here. So that there will be no error if the procedure is called up by a different control type, we first check in the first line whether the Sender is really of the type TListView. If not, we'll break off immediately. If so, we can continue and select all elements of the calling ListView according to the process we discussed above.

ReplyPositiveNegative

About the Author

AvatarYou can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile

 

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

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.