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?