title: TextBox.TextChange to update an onscreen ListBox in C#
body: <p>Now this might not sound horribly cryptic, but I'm a little new to handling on-screen elements and <code>Form</code>s and such in <code>C#</code> so bear with me here. It's also going to be a bit of a long one, as I feel I should provide as much information as possible.</p>
  
  <p>I have a <code>TextBox</code> object, which is added to my <code>fSelect</code> (which is my <code>Form</code> in this case), and it's done like this:</p>
  
  <pre><code>TextBox searchBox = new TextBox();
  fSelect.Controls.Add(searchBox);
  searchBox.Location = new Point(40, 255);
  searchBox.Width = 520;
  searchBox.TextChanged += new EventHandler(searchBox_TextChanged);
  </code></pre>
  
  <p>Now, as you can see every time something changes in the <code>TextBox</code> a certain operation is carried out. That operation would be:</p>
  
  <pre><code>private void searchBox_TextChanged(object sender, EventArgs e)
  {
      TextBox s = (TextBox)sender;
      bool b = false;
      List&lt;string&gt; f = new List&lt;string&gt;();
  
      ListBox updatedLb = new ListBox();
      updatedLb.Size = new System.Drawing.Size(568, 255);
      updatedLb.SelectionMode = SelectionMode.MultiSimple;
  
      foreach (string value in lb.Items)
      {
          if (value.IndexOf(s.Text, StringComparison.OrdinalIgnoreCase) &gt;= 0)
          {
              f.Add(value);
              b = true;
          }
      }
  
      for (int i = 0; i &lt; f.Count; i++)
      {
          updatedLb.Items.Add(f[i]);
       }
  
      s.FindForm().Controls.Remove(lb);
  
      s.FindForm().Controls.Add(updatedLb);
  
  }
  </code></pre>
  
  <p>Looking at that code, there's one element that needs explanation, <code>lb</code>. <code>lb</code> is in this case a <code>public</code> <code>ListBox</code> which contains a set amount of <code>string</code> elements. It is defined as a <code>ListBox</code> outside any method.</p>
  
  <p>Now, in the method my <code>searchBox</code> was defined I filled my <code>lb</code> as follows:</p>
  
  <pre><code>foreach (string value in list)
  {
      lb.Items.Add(value.title);
  }
  </code></pre>
  
  <p>(If it matters, I should also mention that the adding of <code>string</code>s to <code>lb</code> happens before it is inserted into the <code>fSelect</code> <code>Form</code> later.)</p>
  
  <p>Now, for those who haven't guessed my question yet; I wish to present a user with a <code>Form</code> that has a <code>ListBox</code> on it. The elements in this <code>ListBox</code> will be whatever elements contain the <code>string</code> the user types into <code>searchBox</code>, AS the user types it.</p>
  
  <p>The problem is, that the search is only carried out once, and the only thing that is searched for is the first key the user puts in the <code>searchBox</code>. IE: If I was the user and I was going to search for "key", and the first letter was "k", the list would update to show anything with "k" in the title. But when I tried to type the "e" it would not update or change. It would also not go revert back to the old list if the user removes part of the text in <code>searchBox</code>.</p>
  
  <p>How do I go about getting a <code>Form</code> that shows me a <code>ListBox</code> with elements based on what a user entered into <code>searchBox</code>?</p>