55 Votes

Delphi/Lazarus: Delete selected Items from ListBox using DEL-Key

Tip by Stefan Trost | Last update on 2023-01-27 | Created on 2016-01-09

Today, I would like to show how to implement that a TListBox reacts to pressing the delete key (DEL) in Lazarus or Delphi and that accordingly, all selected entries will be removed from the list, if a user presses this key while the ListBox has the focus.

For that, we assign a OnKeyDown-Event to our listbox in which we are using the following code, to fetch the pressed keys:

procedure TForm1.ListBox1KeyDown(Sender: TObject; var Key: Word; 
  Shift: TShiftState);
var
  i: integer;
begin
  if Key = $2E then begin
    Listbox1.Items.BeginUpdate;

    for i := ListBox1.Items.Count - 1 downto 0 do begin
      if ListBox1.Selected[i] then ListBox1.Items.Delete(i);
    end;

    ListBox1.Items.EndUpdate;
  end;
end;

The variable "Key" contains the code of the pressed key. Because we want to react to the DEL key, first of all, we are asking whether the key is $2E. $2E is the virtual key code for the DEL key in hexadecimal writing (the decimal representation is 46 and the value is also stored in the constant VK_DELETE, which we could also use here).

If this check returns true, that means the key was pressed, we are going through all items of the ListBox in order to delete them in the case that they are selected (in this case the property Selected[i] is TRUE for the Item). It is important to loop through the entries backwards (downto), because otherwise under circumstances we would access items in our loop that are no longer existing because we have deleted them before (Listbox1.Items.Count contains the number of all elements when the loop starts, this number is reduced with deleting items). we should use BeginUpdate and EndUpdate in order to ensure that the ListBox will not be repainted after each deletion but only for one time after all items have been checked.

Deletion via the DeleteSelected Function

Newer versions of Delphi and Lazarus make it much easier to delete items from a ListBox, since a function called DeleteSelected has now been added to the TListBox control. This means that we only have to check for the DEL key (as described above) in order to call the DeleteSelected function if this key has been pressed (and do not have to care about the rest):

procedure TForm1.ListBox1KeyDown(Sender: TObject; var Key: Word; 
  Shift: TShiftState);
var
  i: integer;
begin
  if Key = $2E then begin
    Listbox1.DeleteSelected;
  end;
end;

Nevertheless, the method described above still keeps its justification today, since we can expand this code with further tests and checks before the deletion takes place. For example, before deletion, we could first display a dialog and carry out the deletion only if the user has given his okay or we could check for any other conditions, which would not be possible with the mere calling of DeleteSelected.

ReplyPositiveNegativeDateVotes
22 Votes

Thank you!

This is the most helpful post that explains the simplest solution to delete items from a list - without getting access violations!
2018-04-25 at 13:24

ReplyPositive Negative
11 Vote

You do not need to do all this. Because:

ListBox1.DeleteSelected;
2019-10-23 at 10:42

ReplyPositive Negative
11 Vote

Of course, that's the easiest way.

But it depends on the Delphi and Lazarus version whether this function is already available. If someone is using older versions, he or she has to fall back to the manual solution.
2019-10-23 at 15:07

Positive Negative
Reply
Reply

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.