Delphi 7 - How to delete an item from listview using its caption

白昼怎懂夜的黑 提交于 2019-12-11 01:47:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!