Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2306
Contains Method of Items collection
posted

 Hello!

I have a problem with verifying if a certain value is added to a ListView. The items are created with this code:


            UltraListViewItem item = new UltraListViewItem(name, new UltraListViewSubItem[ { new UltraListViewSubItem(size, null) }, null);//size is a number
            item.Key = name;//name is a string
            item.Value = name;
      
            //Control if an item with the same name already exist

           if (ultraListViewDocumenti.Items.Contains(name))
            {
                MessageBox.Show("Already in list!");
                return;
            }

            //Else it is not on the listview so add it to the collection
            ultraListViewDocumenti.Items.Add(item);

 

The problem is that it does not enter on the if block and as a result i get an ArgumentException that say "The element is already added"

Any idea how to fix this?

 

Thank you.

 

Parents
No Data
Reply
  • 69832
    Verified Answer
    Offline posted

    Contains does a reference comparison on each object in the collection, what you want to use is the Exists method, which does a string comparison of the keys.

Children