Results 1 to 14 of 14

Thread: [RESOLVED] [2005] problem with for each loop through listbox items

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Resolved [RESOLVED] [2005] problem with for each loop through listbox items

    Hi

    I am trying to execute this code as part of a backgroundworker thread:

    Code:
            For Each stockedItem As String In lstStock.Items
                For Each toBuyItem As String In lstBuy.Items
     
                    If stockedItem.ToLower = toBuyItem.ToLower Then
                    'my code here
                    End If
     
     
                Next
            Next

    But it is throwing an exception (on the bold/underlined line) saying that "The list this enumerator has been bound to has been modified"..

    can anyone help me solve this problem because i'm not sure what to do..

    thanks

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: [2005] problem with for each loop through listbox items

    Since you want to remove something from one of your ListBoxes you need to loop backward through them in order to avoid that error.

    vb.net Code:
    1. For i As Integer = Me.ListBox1.Items.Count - 1 To 0 Step -1
    2.     If somecondition Then
    3.         Me.ListBox1.Items.RemoveAt(i)
    4.     End If
    5. Next i

    You may also find the ListBox.Items.Contains property easier to work with if you are checking against another collection.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] problem with for each loop through listbox items

    You've left out the most important part!
    Post the "my code here" part.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: [2005] problem with for each loop through listbox items

    Quote Originally Posted by nmadd
    Since you want to remove something from one of your ListBoxes you need to loop backward through them in order to avoid that error.
    huh? :s i'm not trying to remove anything from either listbox..
    i am trying to loop through both listboxes, compare each item against each item from the other listbox, and then if it finds an item which is in BOTH listboxes, then it should execute my code..


    penagate.. here is my full code :

    Code:
            For Each stockedItem As String In lstStock.Items
                For Each toBuyItem As String In lstBuy.Items
    
                    If stockedItem.ToLower = toBuyItem.ToLower Then
                        worker.ReportProgress(35, "Found an item match!")
                        Dim rRegEx2 As Regex
                        Dim mMatch2 As Match
    
                        rRegEx2 = New Regex(myregexhere)
    
                        mMatch2 = rRegEx2.Match(HTML)
    
                        If mMatch2.Success Then
                            worker.ReportProgress(40, "Gathering Information")
    
                            Timer.Enabled = False
    
                            Try
                                Dim number As Integer = lstStock.Items.IndexOf(stockedItem)
                                lstStock.SetSelected(number, True)
                                Dim number2 As Integer = lstBuy.Items.IndexOf(stockedItem)
                                lstBuy.SetSelected(number2, True)
                            Catch
    'if it throws an exception, it just means the letter-case of stockedItem does not match the letter-case of the item in the listbox.. but this won't happen much and it doesn't really matter if it doesn't get selected!
                            End Try
    
    
                            Dim cost As String = mMatch2.Groups(3).ToString
    
                            Dim smartCost As Integer = myModule.SMARTCost(cost)
    
                            continueRefresh = False
    
                            ItemCost = smartCost
                            ItemName = stockedItem
                        Else
                            'a strange error occured if we end up here :s
                            MsgBox("Debug: Error with regex match")
                        End If
                    Else
                        'no matches
                    End If
    
                Next
            Next
    and thanks for helping

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [2005] problem with for each loop through listbox items

    I can only think of one thing.

    Comment out the following line of the Try block:
    VB.NET Code:
    1. lstBuy.SetSelected(number2, True)

    —and see if it runs.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: [2005] problem with for each loop through listbox items

    ah that worked
    thanks for helping!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: [RESOLVED] [2005] problem with for each loop through listbox items

    oh actually i've got another small problem relating to the same thing so maybe you could help me fix that too?


    I wrote this function which is meant to remove duplicate items in a listbox.. so if there is an item there more than once it should delete the others so there's only one of them..

    but it throws the same exception as my other problem :|


    Code:
        Public Function removeDupes(ByVal listbox As Windows.Forms.ListBox)
            Dim removedcount As Integer = 0
            For Each item As String In listbox.Items
                For Each item_compare As String In listbox.Items
                    If item = item_compare Then
                        listbox.Items.Remove(item_compare)
                        removedcount += 1
                    End If
                Next
            Next
            Return removedcount
        End Function
    any idea what i can do to fix this one?

    Edit: also, is there a way to make it so that it ignores case? so it will remove duplicates including when the case is differerent, e.g. anITEM and anItem

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2005] problem with for each loop through listbox items

    Here's a slightly more efficient way:

    Code:
    Public Function removeDupes(ByVal listbox As Windows.Forms.ListBox)
      Dim newItems As New List(Of String)
      
      For Each o As Object In listbox.Items
        Dim s As String = DirectCast(o, String)
        
        If (Not newItems.Contains(s)) _
          newItems.Add(s)
      Next
      
      listbox.Items.Clear()
      
      For Each item As String In newItems: _
        listbox.Items.Add(item)
    End Function

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2005] problem with for each loop through listbox items

    Case-insensitive is a bit less elegant.
    Code:
    Public Function removeDupes(ByVal listbox As Windows.Forms.ListBox)
      Dim newItems As New List(Of String)
      
      For Each o As Object In listbox.Items
        Dim s As String = DirectCast(o, String)
        Dim exists As Boolean = False
        
        For Each item As String In newItems
          If (String.Compare(s, item, True) = 0) Then
            exists = True
            Exit For
          End If
        Next
        
        If (Not Exists) _
          newItems.Add(s)
      Next
      
      listbox.Items.Clear()
      
      For Each item As String In newItems: _
        listbox.Items.Add(item)
    End Function

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: [RESOLVED] [2005] problem with for each loop through listbox items

    Ok thanks but I'm having a problem with it :|

    I think you left out a couple of Next's and an End If and I tried to put them in myself but maybe I did it wrong.. because when I tried it on a list where it should have removed one duplicates, it said it removed 0 and it removed all items from the listbox :S

    Here's the code I tried:

    Code:
        Public Function removeDupes(ByVal listbox As Windows.Forms.ListBox)
            Dim removed As Integer = 0
    
            Dim newItems As New List(Of String)
    
            For Each o As Object In listbox.Items
    
                Dim s As String = DirectCast(o, String)
                Dim exists As Boolean = False
    
                For Each item As String In newItems
                    If (String.Compare(s, item, True) = 0) Then
                        exists = True
                        Exit For
                    End If
    
                    If (Not exists) Then
                        newItems.Add(s)
                    Else
                        removed += 1
                    End If
    
                Next
            Next
    
            listbox.Items.Clear()
    
            For Each item As String In newItems _
            : listbox.Items.Add(item)
            Next
    
            Return removed
    
        End Function

  11. #11
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2005] problem with for each loop through listbox items

    I do not think I left out any Next's or End If's, although it is possible that I made a mistake. Did you try the code exactly as posted?

    To find out how many items were removed, just read the listbox.Items.Count property before and after calling the function.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: [RESOLVED] [2005] problem with for each loop through listbox items

    Quote Originally Posted by penagate
    I do not think I left out any Next's or End If's, although it is possible that I made a mistake. Did you try the code exactly as posted?

    To find out how many items were removed, just read the listbox.Items.Count property before and after calling the function.

    oh,
    yes I tried it exactly as you posted it and it was giving me a couple of errors about missing Next and a missing End If so that's why I tried to add them in :|

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] [2005] problem with for each loop through listbox items

    OK, sorry.
    I left out a "Then".
    Code:
    If (Not Exists) Then _

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: [RESOLVED] [2005] problem with for each loop through listbox items

    oh, thanks a lot
    it's working very nicely now!

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