2014-11-14 82 views
-1

我有一个列表内我想防止列表视图添加项目已经存在,并只允许项目不存在我搜索有关之前,我发布我的问题我找到一些代码,消除重复的项目,但是那不是我的观点,是什么目的要达到一个小例子,例如如何防止在列表视图中添加重复的项目在012phi

listview1.Items.Add.caption := 'item1' 
listview1.Items.Add.subitems.add:= 'content' 

listview1.Items.Add.caption := 'item2' 
listview1.Items.Add.subitems.add:= 'content2' 

listview1.Items.Add.caption := 'item3' 
listview1.Items.Add.subitems.add:= 'content3' 

//duplicated line 
listview1.Items.Add.caption := 'item1'// here what i want to ignore if exist and add any other items comes below 
listview1.Items.Add.subitems.add:= 'content' 


listview1.Items.Add.caption := 'item4' 
listview1.Items.Add.subitems.add:= 'content4' 

就如何实现这一忽略存在的项目,并添加什么都等项目的任何想法?

当前代码

if Command = 'CallThis' then 
    begin 
    if Assigned(MS) then 
    begin 
     SL := TStringList.Create; 
     try 
     SL.LoadFromStream(MS); 
     for I := 0 to SL.Count -1 do 
     begin 
      Line := SL.Strings[I]; 
      ExplodeLine(Line, item, content, num); 
      with vieform.list.Items.Add do 
      begin 
       Caption := StripHTML(item); 
       Subitems.Add(content); 
       Subitems.Add(num) 
      end; 
     end; 
     finally 
     SL.Free; 
     end; 
     MS.Free; 
    end; 
    end; 
+0

我不知道如何实现这个过程 – DelphiStudent 2014-11-14 23:39:09

+1

只要删除,增加了重复的代码。或者真正的代码完全不同? – 2014-11-14 23:44:52

+0

问题中的代码只是iam试图实现的例子,但在我的项目中,我添加项目从列表视图从Tstringlist,我称这个Tsringlist与命令添加到列表视图,但每次我调用命令列表视图添加Tstringlist项目一次又一次。 – DelphiStudent 2014-11-14 23:58:06

回答

5

您不应该使用可视化控件来存储和管理您的数据。列出所有数据并在列表视图或任何其他您喜欢的控件中显示数据。

// class to store data (shortend) 
TMyData = class 
    constructor Create(const Item, Content : string); 
    property Item : string; 
    property Content : string; 
end; 

// list to organize the data 
MyList := TObjectList<TMyData>.Create(
    // comparer, tell the list, when are items equal 
    TComparer<TMyData>.Construct(
    function (const L, R : TMyData) : integer 
    begin 
     Result := CompareStr(L.Item, R.Item); 
    end)); 

// create an item 
MyData := TMyData.Create('item1', 'content1'); 

// check for duplicate in list 
if not MyList.Contains(MyData) then 
    MyList.Add(MyData) 
else 
    MyData.Free; 

// present the list in a ListView 
ListView1.Clear; 
for MyData in MyList do 
begin 
    ListItem := ListView1.Items.Add; 
    ListItem.Data := MyData; // store a reference to the data item 
    ListItem.Caption := MyData.Item; 
    ListItem.SubItems.Add(MyData.Content); 
end; 

这就是所有

+2

更进一步,使用虚拟控制 – 2014-11-15 00:57:44

2

只写你自己的过程,它做所有的工作适合你。也与你的子项目帮助,只是我不知道你试图在你的代码做(这就是我假设你正在尝试做的)...

procedure TForm1.Add(const Caption, Sub: String); 
var 
    I: TListItem; 
    X: Integer; 
begin 
    for X := 0 to ListView1.Items.Count-1 do 
    if SameText(ListView1.Items[X].Caption, Caption) then Exit; 
    I:= ListView1.Items.Add; 
    I.Caption:= Caption; 
    I.SubItems.Add(Sub); 
end; 

然后,你只需这样称呼:

Add('Item1', 'Content'); 
Add('Item2', 'Content2'); 
Add('Item3', 'Content3'); 
Add('Item1', 'Content1'); 

这将导致列表中的3个项目,因为第4个已经存在。

但请注意,这可能并不能真正解决您的真正潜在问题。如果您觉得需要执行此项检查,那么现在可能是重新考虑您的设计的好时机。您使用的方法使我相信您正在使用TListView来存储数据。 UI控件不应该是实际数据的容器,它应该只提供给用户的界面。

+0

不知道为什么有人决定在最后添加我的编辑后收回他们的upvote。 – 2014-11-15 02:38:13

相关问题