使用C#阻止ListView中的双项?

使用C#阻止ListView中的双项?

问题描述:

我们如何访问添加到ListView的项目?使用C#阻止ListView中的双项?

我必须做的事情是:添加一个项目到列表视图。我想检查添加到列表视图的项目是否已经存在于ListView中。

我使用C#和Visual Studio 2005

你可以做这样的事情:

ListViewItem itemToAdd; 
bool exists = false; 
foreach (ListViewItem item in yourListView.Items) 
{ 
    if(item == itemToAdd) 
     exists=true; 
} 

if(!exists) 
    yourListView.Items.Add(itemToAdd); 

在Robban的回答小幅盘整

ListViewItem itemToAdd; 
bool exists = false; 
foreach (ListViewItem item in yourListView.Items) 
{ 
    if(item == itemToAdd) 
    { 
     exists=true; 
     break; // Break the loop if the item found. 
    } 
} 
if(!exists) 
{ 
    yourListView.Items.Add(itemToAdd); 
} 
else 
{ 
    MessageBox.Show("This item already exists"); 
} 

下面将有助于找到一个ListViewItem内的ListView控件一旦你添加它:

string key = <some generated value that defines the key per item>; 
if (!theListViewControl.Items.ContainsKey(key)) 
{ 
    item = theListViewControl.Items.Add(key, "initial text", -1); 

} 

// now we get the list item based on the key, since we already 
// added it if it does not exist 
item = theListViewControl.Items[key]; 
... 

注意 用于将项目添加到ListView项目集合可以是任意唯一值可以将项目的集合中识别ListViewItemkey。例如,它可能是哈希代码值或连接到ListViewItem的对象上的某个属性。

只需添加您的项目,并确保您分配一个名称。然后, 只需使用方法Items集合到 确定它是否存在,就像这样。

for (int i = 0; i < 20; i++) 
{ 
    ListViewItem item = new ListViewItem("Item" + i.ToString("00")); 
    item.Name = "Item"+ i.ToString("00"); 
    listView1.Items.Add(item); 
} 
MessageBox.Show(listView1.Items.ContainsKey("Item00").ToString()); // True 
MessageBox.Show(listView1.Items.ContainsKey("Item20").ToString()); // False 
+0

这似乎是正确实施遏制测试的方法。为什么MS没有明确说明item.Name *是关键?或者更好的是,只需在ListView.Items集合上实现自己的IEquatable接口? Bah ... +1 kripto – IAbstract 2010-01-28 09:53:10

+0

我同意@IAbstract,我的评论是,使用的方法是** ContainsKey **,而不是**包含** ... + 1 – 2015-05-05 21:06:37

ListView类提供了几种不同的方法来确定一个项目存在:

他们可以按以下方式使用:

// assuming you had a pre-existing item 
ListViewItem item = ListView1.FindItemWithText("test"); 
if (!ListView1.Items.Contains(item)) 
{ 
    // doesn't exist, add it 
} 

// or you could find it by the item's text value 
ListViewItem item = ListView1.FindItemWithText("test"); 
if (item != null) 
{ 
    // it exists 
} 
else 
{ 
    // doesn't exist 
} 

// you can also use the overloaded method to match sub items 
ListViewItem item = ListView1.FindItemWithText("world", true, 0); 
+3

工程像魅力,为什么没有这个答案接受? – thecodeassassin 2010-09-08 08:46:20

+0

可能是OP自己解决了这个问题,或者只是发现它对他有效并放弃了这个问题。 – 2014-03-30 21:09:15

在多列的ListView的情况下,你可以使用下面的代码根据任一列防止重复条目:

让我们假设有这样

public class Judge 
    { 
     public string judgename; 
     public bool judgement; 
     public string sequence; 
     public bool author; 
     public int id; 

    } 

一类法官,我想在ListView中添加此类的唯一对象。在这个类中id是唯一的字段,所以我可以借助这个字段来检查ListView中的唯一记录。

Judge judge = new Judge 
       { 
        judgename = comboName.Text, 
        judgement = checkjudgement.Checked, 
        sequence = txtsequence.Text, 
        author = checkauthor.Checked, 
        id = Convert.ToInt32(comboName.SelectedValue) 
       }; 

      ListViewItem lvi = new ListViewItem(judge.judgename); 
      lvi.SubItems.Add(judge.judgement ? "Yes" : "No"); 
      lvi.SubItems.Add(string.IsNullOrEmpty(judge.sequence) ? "" : txtsequence.Text); 
      lvi.SubItems.Add(judge.author ? "Yes" : "No"); 
      lvi.SubItems.Add((judge.id).ToString()); 
      if (listView1.Items.Count != 0) 
      { 
       ListViewItem item = listView1.FindItemWithText(comboName.SelectedValue.ToString(), true, 0); 
       if (item != null) 
       { 
        // it exists 
       } 
       else 
       { 
        // doesn't exist 
       } 

      }