title: Realtime filtering of listbox
body: <p>I would like to be able to filter a listbox containing 1000 strings, each 50 - 4000 characters in length, as the user types in the textbox without a delay.</p>
  
  <p>I'm currently using a timer which updates the listbox after the <code>TextChanged</code> event of the textbox has not been triggered in 300ms.  However, this is quite jerky and the ui sometimes freezes momentarily.</p>
  
  <p>What is the normal way of implementing functionality similar to this?   </p>
  
  <p>Edit: I'm using winforms and .net2.</p>
  
  <p>Thanks</p>
  
  <p>Here is a stripped down version of the code I am currently using:</p>
  
  <pre><code>string separatedSearchString = this.filterTextBox.Text;
  
  List&lt;string&gt; searchStrings = new List&lt;string&gt;(separatedSearchString.Split(new char[] { ';' }, 
                                                StringSplitOptions.RemoveEmptyEntries));
  
  //this is a member variable which is cleared when new data is loaded into the listbox
  if (this.unfilteredItems.Count == 0)
  {
      foreach (IMessage line in this.logMessagesListBox.Items)
      {
          this.unfilteredItems.Add(line);
      }
  }
  
  StringComparison comp = this.IsCaseInsensitive
                          ? StringComparison.OrdinalIgnoreCase
                          : StringComparison.Ordinal;
  
  List&lt;IMessage&gt; resultingFilteredItems = new List&lt;IMessage&gt;();
  
  foreach (IMessage line in this.unfilteredItems)
  {
      string message = line.ToString();
      if(searchStrings.TrueForAll(delegate(string item) { return message.IndexOf(item, comp) &gt;= 0; }))
      {
          resultingFilteredItems.Add(line);
      }
  }
  
  this.logMessagesListBox.BeginUpdate();
  this.logMessagesListBox.Items.Clear();
  this.logMessagesListBox.Items.AddRange(resultingFilteredItems.ToArray());
  this.logMessagesListBox.EndUpdate();
  </code></pre>