Hack Forums
[.NET] Looping through a listbox and removing items - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Looping through a listbox and removing items (/showthread.php?tid=4519486)



Looping through a listbox and removing items - Alien - Nov 2, 2014

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


* - Hawk HF - Nov 2, 2014

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.


* - Only3 - Nov 2, 2014

check b4 u add them > If Not listBox1.Items.Contains
or HashSet > http://msdn.microsoft.com/en-us/library/bb359438(v=vs.110).aspx


* - Odus - Nov 2, 2014

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



* - Alien - Nov 2, 2014

(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



* - Cairns - Nov 3, 2014

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