3 Replies - 9170 Views - Last Post: 22 February 2013 - 01:45 PM Rate Topic: -----

#1 spigniff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-February 13

remove multi selected items from listbox

Posted 22 February 2013 - 02:07 AM

vb.net 2010
I am trying to use multi-select in listbox A to move the selection to listbox B... I can do this with single selections but when using the remove method on a multi-selection (collection) in a loop it crashes. The exception thrown is that it changes the loop control list and it won't allow it. I have tried to create a copy of the selected items list to iterate thru the removal but cant cast it correctly. I found a reference to a list method from the "system.collections.generic" namespace but can't understand the syntax of the "list(of T)" methods. Adding the selection to the other list is easy... but removing the selection from itself is making me crazy. Here is the single selection method I am using to move from listbox B back to A... opposite for A to B

strict is on:
 Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click
        Dim intIndex As Integer
        Dim strJobFolder As String
        intIndex = lstArchiveFolders.SelectedIndex
        strJobFolder = CStr(lstArchiveFolders.SelectedItem)
        If intIndex > -1 Then
            lstArchiveFolders.Items.RemoveAt(intIndex)
            lstBrowseFolders.Items.Add(strJobFolder)
        End If
    End Sub



here is the code that works to add the multi-select collection to the other listbox but the remove method throws the exception:
If lstBrowseFolders.SelectedItems.Count > 0 Then
     For Each strJobFolder In lstBrowseFolders.SelectedItems
         lstArchiveFolders.Items.Add(strJobFolder)
         lstBrowseFolders.Items.Remove(strJobFolder)
     Next
End If



Is This A Good Question/Topic? 0
  • +

Replies To: remove multi selected items from listbox

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13567
  • View blog
  • Posts: 54,133
  • Joined: 12-June 08

Re: remove multi selected items from listbox

Posted 22 February 2013 - 08:24 AM

It's all about knowing your loop's direction, though personally I have some sort of dataset or data collection as the datasource for my listboxes (those tend to be a bit easier on the searching and removing than dealing with flaky objects).

Side note - be explicit in your variable types!
For Each strJobFolder In lstBrowseFolders.SelectedItems

Throw the 'as string' so you know, and the compiler knows, what you are expecting.

My theory - break up your work and it becomes easier to find a solution you can optimize later.

In this case - the copy isn't a big deal, right? Pretty straight forward for loop.
        '-- do the copy from one to the other
        For i As Int32 = ListBox1.SelectedItems.Count - 1 To 0 Step -1
            ListBox2.Items.Add(ListBox1.SelectedItems(i))
        Next



Now we are left with the removal.. well the most straight forward way is take all the selected items and compare them to the items in the list.. if it is a match we know the main list's index for that item and can remove it!
        '-- if we are removing items from a collection that we are using typically I find it best to count
        '-- backwards as I do the removal.  
        For a As Int32 = ListBox1.SelectedItems.Count - 1 To 0 Step -1
            For i As Int32 = ListBox1.Items.Count - 1 To 0 Step -1
                '-- compare the value of the select item to any given item in the list.
                If ListBox1.SelectedItems(a) = ListBox1.Items(i) Then
                    '-- remove the item by the index we found
                    ListBox1.Items.RemoveAt(i)
                    '-- exit the inner for loop
                    Exit For
                End If
            Next
        Next

Was This Post Helpful? 1
  • +
  • -

#3 spigniff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-February 13

Re: remove multi selected items from listbox

Posted 22 February 2013 - 01:19 PM

You nailed it... thanks. I tried many variations of your solution to no avail and felt it was more important to understand the "why" than the actual "how" after seeing your solution. Any use of the list (such as "for each" ) will force a change in the controls at time of removal resulting in a fail. Any use of each item (as opposed to its index) forces a mismatch in casting resulting in a fail. Your approach compares "whatever" at index "x" with "whatever" at index "y" so casting and using the list to loop thru never come into play. "Strict" forced me to use "is" instead of "=" but that was fine. After I thought about my two lists I realized "I never want an item to appear on both lists..... therefore..." I can use the target list receiving the items to compare back and remove any duplicates I had just created. Lots of wasted resources on a long list (for example adding 2 new items to an existing list of 200) but no more than index stepping thru a long list.

Here is what I ended up with - let me know if you see any landmines:

If lstBrowseFolders.SelectedItems.Count > 0 Then
            For Each strJobFolder In lstBrowseFolders.SelectedItems
                lstArchiveFolders.Items.Add(strJobFolder)
            Next

            For Each strJobFolder In lstArchiveFolders.Items
                lstBrowseFolders.Items.Remove(strJobFolder)
            Next

            'For a As Int32 = lstBrowseFolders.SelectedItems.Count - 1 To 0 Step -1
            '    For i As Int32 = lstBrowseFolders.Items.Count - 1 To 0 Step -1
            '        '-- compare the value of the select item to any given item in the list.  
            '        If lstBrowseFolders.SelectedItems(a) Is lstBrowseFolders.Items(i) Then
            '            '-- remove the item by the index we found  
            '            lstBrowseFolders.Items.RemoveAt(i)
            '            '-- exit the inner for loop  
            '            Exit For
            '        End If
            '    Next
            'Next
        End If


Was This Post Helpful? 0
  • +
  • -

#4 spigniff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-February 13

Re: remove multi selected items from listbox

Posted 22 February 2013 - 01:45 PM

ps - I agree about always declaring the data type but was not accepting anything other than object... I added the string type at the end of all this and now it likes it. I think if I were to try to use the string it would then throw the cast exception.. another mystery dealing with the "collection" type that will need to wait until I am allot more accomplished to resolve... thanks again.

' if anything was selected from first list then add each selection to the other list
        If lstBrowseFolders.SelectedItems.Count > 0 Then
            For Each strJobFolder As String In lstBrowseFolders.SelectedItems
                lstArchiveFolders.Items.Add(strJobFolder)
            Next

            ' compare modified list back to first list and remove the duplicates from first list
            For Each strJobFolder As String In lstArchiveFolders.Items
                lstBrowseFolders.Items.Remove(strJobFolder)
            Next


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1