code_junkie

C# How do I do I loop through items in a list box and then remove that item?

Nov 13th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. foreach (string s in listBox1.Items)
  2. {
  3. MessageBox.Show(s);
  4. //do stuff with (s);
  5. listBox1.Items.Remove(s);
  6. }
  7.  
  8. listBox1.BeginUpdate();
  9. try {
  10. for(int i = listBox1.Items.Count - 1; i >= 0 ; i--) {
  11. // do with listBox1.Items[i]
  12.  
  13. listBox1.Items.RemoveAt(i);
  14. }
  15. } finally {
  16. listBox1.EndUpdate();
  17. }
  18.  
  19. List<string> removals = new List<string>();
  20. foreach (string s in listBox1.Items)
  21. {
  22. MessageBox.Show(s);
  23. //do stuff with (s);
  24. removals.Add(s);
  25. }
  26.  
  27. foreach (string s in removals)
  28. {
  29. listBox1.Items.Remove(s);
  30. }
  31.  
  32. while (listBox1.Items.Count > 0)
  33. {
  34. string s = listBox1.Items[0] as string;
  35. // do something with s
  36. listBox1.Items.RemoveAt(0);
  37. }
  38.  
  39. for (int i = 0; i < listBox1.Items.Count; ++i)
  40. {
  41. // ...
  42. listBox1.Items.RemoveAt(i--);
  43. }
  44.  
  45. for i as integer= list.items.count-1 to 0 step -1
  46. ....
  47. list.items.removeat(i)
  48. next
  49.  
  50. for (var i == list.Items.Count - 1; i >= 0; i--)
  51. {
  52. list.Items.RemoveAt(i);
  53. }
  54.  
  55. foreach(var s in listBox1.Items.ToArray())
  56. {
  57. MessageBox.Show(s);
  58. //do stuff with (s);
  59. listBox1.Items.Remove(s);
  60. }
Add Comment
Please, Sign In to add comment