How to delete items from a ComboBox at runtime using C# / WinForms
<p>I have a Combo Box that I can already add items to and I want to be able to remove the item I have selected when I hit the delete key.</p>

<p>Here is the code I am using now.</p>

<pre><code>    private void commandComboBox_KeyDown(object sender, KeyEventArgs e)
    {
        var myComboBox = (ComboBox)sender;
        string text = myComboBox.Text;
        if (e.KeyCode == Keys.Enter)
        {
            myComboBox.Items.Add(myComboBox.Text); // Add
        }

        if (e.KeyCode == Keys.Delete)
        {
            myComboBox.Items.Remove(myComboBox.SelectedItem);
        }
    }
</code></pre>

<p>When I click in the combobox and start typing and then hit enter I hear a windows sound (not sure which one) and then the item is added to the list.
When I hit the dropdown button I see the item there with the text I entered above. When I hit delete the item goes away (at least I think it does) and then when I click somewhere else I get this exception </p>

<p><img src="https://i.stack.imgur.com/bw90z.png" alt=""></p>

<blockquote>
  <p>System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index'.<br>
  Parameter name: index</p>
</blockquote>

<p>Also when I hit the dropdown button I still see the empty spaces </p>

<p><img src="https://i.stack.imgur.com/C0oaz.png" alt=""></p>

<p>So my question is how do I properly delete items from a ComboBox :)</p>

<p>Also if there is something better then a ComboBox for this kind of thing plz mention them as well, TY</p>