title: Stop Button Causing ListBox to Deselect
body: <p>I have a button in place so that when I click it, it deletes the selected <code>ListItem</code> from the <code>ListBox</code> called <code>listBoxNames</code> and removes the selected <code>Person</code> object from a list named <code>people</code>. </p>
  
  <p>However, after clicking the delete button an error message pops up telling me that the index is out of range. This leads me to believe that upon clicking the button, it has deselected the <code>ListBox</code> and so <code>listBoxNames.SelectedIndex</code> returns -1.</p>
  
  <p>Is there anyway around this or have I made some stupid mistake?</p>
  
  <pre><code>private void deleteEntryBtn_Click(object sender, RoutedEventArgs e)
      {
          try
          {
              listBoxNames.Items.Remove(listBoxNames.SelectedItems[0]);
              people.RemoveAt(listBoxNames.SelectedIndex);
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  </code></pre>