title: VB.NET List Box Selected Index
body: <p>I seem to be having issues with the selected index on a list box.</p>
  
  <p>The list box is having various items inserted depending on the user selection. An example would be:</p>
  
  <pre><code>LiIndex = ListBox1.Items.Count      
  ListBox1.Items.Insert(LiIndex, "Item1")
  
  LiIndex = ListBox1.Items.Count      
  ListBox1.Items.Insert(LiIndex, "AND")
  
  LiIndex = ListBox1.Items.Count      
  ListBox1.Items.Insert(LiIndex, "Item2")
  
  LiIndex = ListBox1.Items.Count      
  ListBox1.Items.Insert(LiIndex, "AND")
  
  LiIndex = ListBox1.Items.Count      
  ListBox1.Items.Insert(LiIndex, "Item3")
  </code></pre>
  
  <p>This all work and displays without a problem. The issue I have is if I select the second of the two AND's. If I click the second "AND" in the list and then a button to fire a method, the selected index is always the index of the first "AND".</p>
  
  <pre><code>Dim listIndex as integer = ListBox1.SelectedIndex
  </code></pre>
  
  <p>I can't work out why, the listbox itself will always show the second one as selected, but the action will happen against the first one.</p>
  
  <p>Any ideas as to where I am going wrong would be greatly appreciated.</p>