How prevent duplicate items listView C#

后端 未结 5 683
终归单人心
终归单人心 2020-12-19 07:50

I am using Windows Forms. With this code I add items to listView from comboBox.

ListViewItem lvi = new ListViewItem();         


        
5条回答
  •  萌比男神i
    2020-12-19 08:11

    You should be using ContainsKey(string key) instead of Contains(ListViewItem item)

    var txt = comboBox1.Text;
    
    if (!listView1.Items.ContainsKey(txt))
    {
        lvi.Text = txt;
    
        // this is the key that ContainsKey uses. you might want to use the value 
        // of the ComboBox or something else, depending the combobox is freetext 
        // or regarding your scenario.
        lvi.Name = txt;
    
        lvi.SubItems.Add("");
        lvi.SubItems.Add("");
        lvi.SubItems.Add("");
        lvi.SubItems.Add("");
    
        listView1.Items.Add(lvi);
    }
    

提交回复
热议问题