title: Selecting multiple Listbox items through code
body: <p>Hi there I have searched for a while now and can't seem to find a solution to my problem, I have tried multiple methods to select multiple items in my listbox through code however none have worked, The best result I got was 1 selected item in my listbox.</p>
  
  <p>Basically I want to select multiple items of the same value.</p>
  
  <p>below is my code, sorry if I seem newbie but I am new to programming and still learning basic stuff.</p>
  
  <pre><code> foreach (string p in listBox1.Items)
   {
             if (p == searchstring) 
             {
                   index = listBox1.Items.IndexOf(p);
                   listBox1.SetSelected(index,true);
  
             }
   }
  </code></pre>
  
  <p>So as you can see I am trying to tell the program to loop through all the items in my listbox, and for every item that equals "searchstring" get the index and set it as selected.</p>
  
  <p>However all this code does is select the first item in the list that equals "searchstring" makes it selected and stops, it doesn't iterate through all the "searchstring" items.</p>