title: Searchable Listbox using two different item collections
body: <p>So, I have:</p>
  
  <pre><code>Listbox l = new Listbox();
  </code></pre>
  
  <p>and</p>
  
  <pre><code>private ListBox.ObjectCollection _collection = new ListBox.ObjectCollection( l );
  </code></pre>
  
  <p>This is because I want the listbox to be searchable, meaning that you can filter out items by typing into a textfield and I need two different collections to do this, the normal listbox item list that's painted by the listbox and the 'original' data source. I've got this working very well.
  However, when calling the .Clear() method on the _collection, sometimes I get the following exception:</p>
  
  <pre><code>Object reference not set to an instance of an object.
  </code></pre>
  
  <p>and the callstack looks like so:</p>
  
  <pre><code>at System.Windows.Forms.ListBox.ItemArray.GetItem(Int32 virtualIndex, Int32 stateMask)
  at System.Windows.Forms.ListBox.ObjectCollection.ClearInternal()
  at System.Windows.Forms.ListBox.ObjectCollection.Clear()
  </code></pre>
  
  <p>The only thing I can imagine happening here is that some of the items from the actual ObjectCollection for the Listbox has been sorted away from the .Items list and the Clear method of the ListBox.ObjectCollection, resulting in the _collection somehow trying to clear items that doesn't exist anymore in the .Items collection. What can I do?</p>
  
  <p>This might be the wrong way to implement what I'm interested in, so I'm open to suggestions for that as well.</p>
  
  <p>Edits:
  02/12-12
  Ordering was already turned off as per the comment and I've tried figuring out exactly when it happens but that's been near impossible as it seems to require some random combination of values from other textboxes, comboboxes, clicking a little with the mouse and then finally selecting something in the listbox. 
  According to lines 3094 - 3110 of this <a href="http://www.dotnetframework.org/default.aspx/4@0/4@0/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/fx/src/WinForms/Managed/System/WinForms/ListBox@cs/1305376/ListBox@cs" rel="nofollow">link</a>, clearInternal seems to check the count of the listbox owner and then removing the same amount of items from its InnerArray. I'm pondering whether this is why it tries to remove elements that aren't actually present in the InnerArray, resulting in a null reference. But it seems like each of the two collections should both check the same listbox and remove items accordingly. The _collection may have more items than the listbox, but then it should just try to remove the same amount as it is in the listbox and just end up being not cleared, it shouldn't try to remove elements that aren't existent.</p>
  
  <p>09/06/15
  Still a problem and I've recently been trying to dig into it a bit more but I've run out of ideas to try my luck on. Anyone else got some?</p>