[RESOLVED] [2008] Remove Multiple Items from a Listbox and from a List
Hi!!
I have piece of code that gets all the files from a folder and adds their path to a List. Then I have a loop that loops through the list and adds the filenames to Listbox.
Code:
Dim FilesMusicas As New List(Of String)
FilesMusicas.AddRange(IO.Directory.GetFiles("C:\MyPath", "*.*mp3"))
For a As Integer = 0 To FilesMusicas.Count - 1
FicheirosMusicas.Items.Add(IO.Path.GetFileNameWithoutExtension(FilesMusicas(a)))
Next
(FicheirosMusicas is the Listbox)
Now my problem is that I want to remove multiple items from the Listbox but at the same time the correpondent items on the List. i have this code that should work, but eveytime i run it, it gives me the error "Index was outside the bounds of the array." on the FilesMusicas.RemoveAt(.SelectedIndices(0)) line.
Code:
With FicheirosMusicas
.BeginUpdate()
While .SelectedIndices.Count > 0
.Items.RemoveAt(.SelectedIndices(0))
FilesMusicas.RemoveAt(.SelectedIndices(0))
End While
.EndUpdate()
End With
Why does this happen?
Re: [2008] Remove Multiple Items from a Listbox and from a List
Dont forget you are removing the item from the listbox before you try and remove it from the list so once its gone from the listbox there is no selected item.
remove it from the list first before the listbox.
Re: [2008] Remove Multiple Items from a Listbox and from a List
One small detail can make it all wrong. Thks a lot.
Re: [RESOLVED] [2008] Remove Multiple Items from a Listbox and from a List
Yes, your indices are going to messed up.
You could make it all a bit easier by using the list as a binding source. Have a look at the BindingList class. Then all you have to do is set the DataSource property to the BindingList then you just worry about deleting from the list. Have a look here: http://msdn.microsoft.com/en-us/library/ms132679.aspx