How does ListView.ItemCollection.Contains() work?
Posted
by Inno
on Stack Overflow
See other posts from Stack Overflow
or by Inno
Published on 2010-03-25T10:41:09Z
Indexed on
2010/03/25
10:43 UTC
Read the original article
Hit count: 555
Hi everyone,
I'm copying ListViewItems from one ListView to another, sth. like:
foreach (ListViewItem item in usersListView.SelectedItems) {
selectedUsersListView.Items.Add((ListViewItem)item.Clone());
}
If I want to use ListView.ItemCollection.Contains() to determine if an item was already copied I always get false:
foreach (ListViewItem item in usersListView.SelectedItems) {
if (!selectedUsersListView.Items.Contains(item) { // always !false
selectedUsersListView.Items.Add((ListViewItem)item.Clone());
}
}
I did the following to solve my problem:
foreach (ListViewItem item in usersListView.SelectedItems) {
ListViewItem newItem = (ListViewItem)item.Clone();
newItem.Name = newItem.Text;
if (!selectedUsersListView.Items.ContainsKey(newItem.Name) { // this works
selectedUsersListView.Items.Add(newItem);
}
}
So, it's ok that this solves my problem but I still have no idea why ListView.ItemCollection.Contains() doesn't work...
How does ListView.ItemCollection.Contains() identify if an item already exists?
How do ListViewItems have to be initialised that ListView.ItemCollection.Contains() (not ListView.ItemCollection.ContainsKey()) is able to identify them?
© Stack Overflow or respective owner