About ListView1.Items.Contains

  • Thread starter Thread starter yxq
  • Start date Start date
Y

yxq

Hello,
There is one item(text=123456, no subitem) in ListView1, why the code below
return FALSE?

MessageBox.Show(ListView1.Items.Contains(New ListViewItem("123456")))

Why?

Thank you
 
yxq said:
Hello,
There is one item(text=123456, no subitem) in ListView1, why the
code below return FALSE?

MessageBox.Show(ListView1.Items.Contains(New
ListViewItem("123456")))

Why?


The Listview does not contain the item because it is a new item that could
not have been added to the listview before because it's just created before
calling Contains. Maybe there's an item with the same text in the listview,
but it is not the same object. You have to write a loop to check whether an
item is having the same text is already in the listview.


Armin
 
Hi,

The ListView.Items.Contains() method takes an (already existing)
ListViewItem as argument, and checks for that. In your code, you
haven't added the ListViewItem to the ListView at all.

The following code will return True :
 
Back
Top