Home All Groups Group Topic Archive Search About

About ListView1.Items.Contains

Author
17 Mar 2006 6:56 AM
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

Author
17 Mar 2006 11:09 AM
Armin Zingler
"yxq" <ga***@163.net> schrieb
> 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
Author
17 Mar 2006 11:31 AM
Cerebrus
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 :
-----------------------------------------------------
Dim lvi as New ListViewItem("123456")
ListView1.Items.Add(lvi)
MessageBox.Show(ListView1.Items.Contains(lvi))
-----------------------------------------------------

Regards,

Cerebrus.
Author
17 Mar 2006 2:12 PM
yxq
Thank you.

Show quoteHide quote
"Cerebrus" <zorg***@sify.com> дÈëÏûÏ¢ÐÂÎÅ:1142595114.152665.118***@v46g2000cwv.googlegroups.com...
> 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 :
> -----------------------------------------------------
> Dim lvi as New ListViewItem("123456")
> ListView1.Items.Add(lvi)
> MessageBox.Show(ListView1.Items.Contains(lvi))
> -----------------------------------------------------
>
> Regards,
>
> Cerebrus.
>