![]() |
|
[.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()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. ![]() Code Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click* - 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. I don't know why..... but when I loaded your code... Code For Each Item As String In ListBox2.ItemsIt 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 |