|
-
Aug 7th, 2007, 01:32 PM
#1
Thread Starter
Addicted Member
[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
-
Aug 7th, 2007, 01:40 PM
#2
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:
For i As Integer = Me.ListBox1.Items.Count - 1 To 0 Step -1
If somecondition Then
Me.ListBox1.Items.RemoveAt(i)
End If
Next i
You may also find the ListBox.Items.Contains property easier to work with if you are checking against another collection.
-
Aug 7th, 2007, 01:50 PM
#3
Re: [2005] problem with for each loop through listbox items
You've left out the most important part!
Post the "my code here" part.
-
Aug 7th, 2007, 02:07 PM
#4
Thread Starter
Addicted Member
Re: [2005] problem with for each loop through listbox items
 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
-
Aug 7th, 2007, 02:16 PM
#5
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:
lstBuy.SetSelected(number2, True)
—and see if it runs.
-
Aug 7th, 2007, 02:20 PM
#6
Thread Starter
Addicted Member
Re: [2005] problem with for each loop through listbox items
ah that worked 
thanks for helping!
-
Aug 7th, 2007, 02:24 PM
#7
Thread Starter
Addicted Member
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
-
Aug 7th, 2007, 02:36 PM
#8
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
-
Aug 7th, 2007, 02:42 PM
#9
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
-
Aug 7th, 2007, 02:55 PM
#10
Thread Starter
Addicted Member
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
-
Aug 7th, 2007, 02:58 PM
#11
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.
-
Aug 7th, 2007, 03:14 PM
#12
Thread Starter
Addicted Member
Re: [RESOLVED] [2005] problem with for each loop through listbox items
 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 :|
-
Aug 7th, 2007, 03:17 PM
#13
Re: [RESOLVED] [2005] problem with for each loop through listbox items
OK, sorry.
I left out a "Then".
Code:
If (Not Exists) Then _
-
Aug 7th, 2007, 03:39 PM
#14
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|