问题
i'm trying to delete a listview item based into caption, but I can not find a solution for this, the only way I can delete an item is using the index:
listview1.Items.Delete (0);
Can anyone help me to delete an item through the caption?
回答1:
You can use something like this, which attempts to locate a ListItem
with the caption Item 2
, and deletes it if it find it:
procedure TForm1.Button1Click(Sender: TObject);
var
LI: TListItem;
begin
LI := ListView1.FindCaption(0, 'Item 2', False, True, False);
if Assigned(LI) then
begin
ListView1.Selected := LI;
ListView1.DeleteSelected;
end;
end;
An alternative which does not require you to select the item first would be to delete the found item by its Index
:
procedure TForm1.Button2Click(Sender: TObject);
var
LI: TListItem;
begin
LI := ListView1.FindCaption(0, 'Item 2', False, True, False);
if Assigned(LI) then
ListView1.Items.Delete(LI.Index);
end;
来源:https://stackoverflow.com/questions/19427876/delphi-7-how-to-delete-an-item-from-listview-using-its-caption