Ok i want to remove all selecteditems i tried to use this loop but it didn't work
How do i do it because that just didn't do anythingCode:Dim a As String
For Each a In History_Textbox.SelectedItems
History_Textbox.Items.Remove(a)
Next
Printable View
Ok i want to remove all selecteditems i tried to use this loop but it didn't work
How do i do it because that just didn't do anythingCode:Dim a As String
For Each a In History_Textbox.SelectedItems
History_Textbox.Items.Remove(a)
Next
You can't modify a collection that is enumerated with 'for each'. Otherwise you might end up retrieving objects that no longer exist, exactly what you are doing now. What you can do, however, is the following:
Or alternatively:Code:For Each Object o In _
From Object x In History_TextBox.SelectedItems _
OrderBy History_TextBox.Items.IndexOf(x) Descending _
Select x
History_TextBox.Items.Remove(o)
Next
One is LINQ, the other is reverse looping through indices. In order to ensure that the indices remain the same, you must remove items from the back to the front.Code:For i As Integer = History_TextBox.SelectedIndices.Count - 1 To 0 Step -1
History_TextBox.Items.RemoveAt(History_TextBox.SelectedIndices(i))
Next
Use Items.RemoveAt(a)
thankyou for help,worked perfectlyCode:For i As Integer = History_TextBox.SelectedIndices.Count - 1 To 0 Step -1
History_TextBox.Items.RemoveAt(History_TextBox.SelectedIndices(i))
Next