title: C# - Remove all items from listbox
body: <p>I have a C# winform that uses a listbox with a bound list for the datasource. The list is created from a text file on the computer. I'm trying to create a "Remove all" button for this listbox and am having a little trouble. </p>
  
  <p>First, here is the relevant code:</p>
  
  <pre><code>private void btnRemoveAll_Click(object sender, EventArgs e)
      {
          // Use a binding source to keep the listbox updated with all items
          // that we add
          BindingSource bindingSource = (BindingSource)listBox1.DataSource;
  
          // There doesn't seem to be a method for purging the entire source,
          // so going to try a workaround using the main list.
          List&lt;string&gt; copy_items = items;
          foreach (String item in copy_items)
          {
              bindingSource.Remove(item);
          }
      }
  </code></pre>
  
  <p>I've tried foreaching the bindingSource, but it gives an enumeration error and just won't work. As far as I can tell, there's not code to just purge an entire source, so I tried going through the List itself and removing them via the item name, but that doesn't work either since the foreach actually returns an object or something and not a string.</p>
  
  <p>Any suggestions on how to do this? </p>