ListView Remove Items

swingard

Member
Joined
Dec 21, 2006
Messages
6
Programming Experience
1-3
This is driving me nuts! It seems so easy but it's just not working! :mad:

I'm using an array to see if the item is found in one listView and if it is, add it to another listView and delete the entry in the first. It works fine except for the deleting part. It works fine in the 1.1 framework, but gives and InvalidOperationException in the Compact Framework. Any ideas???

VB.NET:
For Each item As ListViewItem In todoListView.Items
            If array.IndexOf(array, item.Text, 0, array.Length) <> -1 Then

                doneItem = New ListViewItem(item.Text)
                doneItem.SubItems.Add(item.SubItems(1).Text)
                doneItem.SubItems.Add(item.SubItems(2).Text)
                doneListView.Items.Add(doneItem)

                todoListView.Items.Remove(item)
            End If
Next
 
Last edited:
Solution

Well, I kind of figured it out. At least it does what I want it to anyway...

VB.NET:
'Check each item in the "todo" list and compare it to the array;
        'Add values found both in the array and the list to the "done" list
        For Each item As ListViewItem In todoListView.Items
            If array.IndexOf(array, item.Text, 0, array.Length) <> -1 Then
                doneItem = New ListViewItem(item.Text)
                doneItem.SubItems.Add(item.SubItems(1).Text)
                doneItem.SubItems.Add(item.SubItems(2).Text)
                doneListView.Items.Add(doneItem)
            End If
        Next

        'Go through the done list and compare it with the todo list
        'Remove duplicates from todo list
        For Each item As ListViewItem In doneListView.Items
            For i As Long = 0 To todoListView.Items.Count - 1
                Try
                    If item.Text = todoListView.Items(i).Text Then
                        todoListView.Items.Remove(todoListView.Items(i))
                    End If
                Catch ex As Exception
                    Exit For
                End Try

            Next
        Next
 
Here's a shorter way of Removing items in a FOR EACH

For Each item As String In New List(Of String)(shortlist)
If item.Contains("RemoveMe") Then
shortlist.Remove(item)
End If
Next

Here you are creating a new list with the data from shortlist and cycling through that, while removing items in shortlist.
 

Latest posts

Back
Top