Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Looping through a listbox and removing items

This issue has come up rather unexpectedly and I can't seem to figure out what exactly the problem is. So, basically.. I am trying to loop through a listbox and remove any items that match up with the items/contents of another listbox. My code works fine.. up until I import a list of items that consist of over 300 into the first listbox then nothing gets removed. I know it sounds a little bit confusing.. but just take a look at my code and you can probably see what I'm talking about.

Code
Using FD As New OpenFileDialog()
            FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
            If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
                WordBox.Items.Clear()
                WordBox.Items.AddRange(IO.File.ReadAllLines(FD.FileName))

                For Each item In Listbox2.Items
                    For i As Integer = WordBox.Items.Count - 1 To 0 Step -1
                        If WordBox.Items(i).Contains(item.ToString) Then
                            WordBox.Items.RemoveAt(i)
                        End If
                    Next
                Next

            
            End If

          
        End Using

Any help is appreciated! Thanks
Do not deal with me off site. I never deal offsite unless I have verified via PM.
I don't think you can edit the contents of the listbox while looping through it.

Try using an array to temporarily store the new contents of listbox2, then setting it after the loop has been completed.
check b4 u add them > If Not listBox1.Items.Contains
or HashSet > http://msdn.microsoft.com/en-us/library/....110).aspx
I wasn't able to recreate your problem even when using your code with a txt at 450 lines.
[Image: 23gSeM8.gif]

Code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      For Each Item As String In ListBox2.Items
      For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
            If ListBox1.Items(i) = Item Then ListBox1.Items.RemoveAt(i)
      Next
      Next
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ListBox1.Items.AddRange(IO.File.ReadAllLines("C:\Users\Odus\Desktop\Test.txt"))
    End Sub
(Nov 2, 2014 03:32 PM)Odus Wrote:  I wasn't able to recreate your problem even when using your code with a txt at 450 lines.
[Image: 23gSeM8.gif]

Code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      For Each Item As String In ListBox2.Items
      For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
      If ListBox1.Items(i) = Item Then ListBox1.Items.RemoveAt(i)
      Next
      Next
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      ListBox1.Items.AddRange(IO.File.ReadAllLines("C:\Users\Odus\Desktop\Test.txt"))
    End Sub

I don't know why..... but when I loaded your code...

Code
For Each Item As String In ListBox2.Items
      For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
      If ListBox1.Items(i) = Item Then ListBox1.Items.RemoveAt(i)
      Next
      Next

It seems to have fixed the problem.. lol

The only real difference I can see that may of fixed it is..

Code
For Each Item As String In ListBox2.Items
Do not deal with me off site. I never deal offsite unless I have verified via PM.
For Each won't work whilst you are removing an item, at least it doesn't for a collection class because when you remove an item the collection is being edited.

Use a For Count loop and do it, and count backwards using step -1


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by OctaviaAug 26, 2022 11:36 AM 0 170 Aug 26, 2022 11:36 AM
Last Post: Octavia
[Java] by Sterling Malory ArcherJul 11, 2021 03:48 PM 3 220 Jul 11, 2021 03:48 PM
Last Post: CheesyBread
[Python] by Smirking_EmojiDec 5, 2019 09:26 PM 13 388 Dec 5, 2019 09:26 PM
Last Post: magicparty