title: Deleting listbox's items using context menu c#
body: <p>So I have a listbox in my program and I want the user to be able to delete a specific item in it by accessing its context menu and clicking delete. Everything seems to be working fine if I delete its items in order (from top item to the last item). However, I get a debugging error when I don't delete the items in order. I want to know how will I be able to delete an item of my choice.</p>
  
  <p>This is the code that will execute once the user right clicks an item in the listbox:</p>
  
  <pre><code>public void listBox_MouseDown(object sender, MouseEventArgs e)
  {
      if (e.Button == System.Windows.Forms.MouseButtons.Right)
      {
          index = listBox.IndexFromPoint(e.Location);
          {
              if (index == listBox.SelectedIndex)
              {
                  contextMenuStrip1.Show(); 
              }
          }
      }
  }
  </code></pre>
  
  <p>This is the click event handler:</p>
  
  <pre><code>private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
  {
      listBox.Items.Remove(listBox.SelectedItems[index]);
  }
  </code></pre>
  
  <p>This is the error message from the IDE:
  <strong>An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Windows.Forms.dll
  Additional information: Index was outside the bounds of the array.</strong></p>