wa22guy Posted October 26, 2004 Share Posted October 26, 2004 Ok, I want to clear just selected items in my listbox in C#. Everytime I try to do it thought it throws an Enumeration error. Here's my code: foreach (string item in lstFiles.SelectedItems) { lstFiles.Items.Remove(item); } What do I do?? Link to comment Share on other sites More sharing options...
0 lexecutil Posted October 26, 2004 Share Posted October 26, 2004 (edited) You're going to have to loop backwards and delete them like that. In a foreach you cant modify the collection. :) Dan EDIT: whipped up some code for ya: ? for (int i = listBox1.SelectedItems.Count - 1; i > -1; i--) ? { ? ?listBox1.Items.Remove(listBox1.SelectedItems[i]); ? } Edited October 26, 2004 by dannyres Link to comment Share on other sites More sharing options...
0 wa22guy Posted October 26, 2004 Author Share Posted October 26, 2004 thanks man, huge help! :) Link to comment Share on other sites More sharing options...
Question
wa22guy
Ok, I want to clear just selected items in my listbox in C#. Everytime I try to do it thought it throws an Enumeration error. Here's my code:
foreach (string item in lstFiles.SelectedItems) { lstFiles.Items.Remove(item); }What do I do??
Link to comment
Share on other sites
2 answers to this question
Recommended Posts