2014-11-14 36 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

相關問題