Results 1 to 4 of 4

Thread: [RESOLVED] [2008] Remove Multiple Items from a Listbox and from a List

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved [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?
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    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.

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: [2008] Remove Multiple Items from a Listbox and from a List

    One small detail can make it all wrong. Thks a lot.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4
    Hyperactive Member
    Join Date
    Mar 2002
    Location
    Boston, MA
    Posts
    391

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width