Delphi/Lazarus: Delete selected items with DEL-key from ListView
Tip by Delphian | 2013-10-05 at 13:11
Having a TListView with some selected items in Delphi or Lazarus and pressing the "DEL" or "DELETE" key on the keyboard, first of all, nothing happens.
So, what can be do to implement this function of deleting the marked entries from our ListView? We have to add the following code to the OnKeyDown Event of the ListView:
procedure Tprog.ListView1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); var i: integer; begin if key = $2E then begin ListView1.Items.BeginUpdate; for i := ListView1.Items.Count - 1 downto 0 do if ListView1.Items[i].Selected then ListView1.Items.Delete(i); ListView1.Items.EndUpdate; end; end;
First, we have to look at which button has been pressed. If it is the delete button (this key has a value of $2E), we have to go through all entries currently marked (selected) in order to delete them one by one.
Here, it is important to go through the items in reverse order (downto loop). The reason is because otherwise, when deleting some entries at the beginning, the number of indices of the items actually in the ListView are no more matching our number of items in the loop.
About the Author
The author has not added a profile short description yet.
Show Profile
Related Topics
Delphi/Lazarus: Select all with CTRL+A in ListView
Tip | 0 Comments
Delphi/Lazarus: Delete selected Items from ListBox using DEL-Key
Tip | 3 Comments
MySQL: Delete Data from Table - Difference between TRUNCATE, DELETE and DROP
Tutorial | 0 Comments
Lazarus: Match FormDropFiles with a specific ListView
Tutorial | 0 Comments
Delphi/Lazarus: Is the ALT, SHIFT or CTRL key pressed?
Tutorial | 0 Comments
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.