简体   繁体   中英

How to delete items from a ComboBox at runtime using C# / WinForms

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.

Here is the code I am using now.

    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);
        }
    }

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

System.ArgumentOutOfRangeException: InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index

Also when I hit the dropdown button I still see the empty spaces

So my question is how do I properly delete items from a ComboBox :)

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

The Windows sound you hear is actually an error beep. Combo boxes do not accept enter key presses, so it's beeping at you "no!" Your code also runs, of course, adding the item, but that doesn't change the fact that the combo box considers you pressing Enter when it has the focus to be an error. If you are intent on the current design, you need to eat the Enter key press after you've received it so that the combo box doesn't go on to try to process it. To do so, set e.SuppressKeyPress to true.

The exception you get is because you've deleted all of the items in the combo box, but some other section of your code tries to get the text of item #0 (the first item). There is no first item because you deleted it, so an exception is thrown. I'm not sure what code it is that's responsible for this, since I can't see it, but I'm guessing you have written a handler for something like the SelectedIndexChanged event.

Indeed, this is a very unusual interface. The purpose of a combo box is to present the user with a list of choices, not to allow them to type in multiple choices. If you want that, use a multi-line text box. At least that way, they'll be able to see all the things that they've entered.

Or, you could use the classic interface idiom for this, where there is a textbox to type into that works with an Add button to add the typed text to a ListBox control. A Delete button deletes the currently selected item in the ListBox. A Clear button clears all of the items in the ListBox. Yes, it is as confusing to use as it is to explain. Avoid these whenever possible. They were more popular in the bad old days of UI design.

Also when I hit the dropdown button I still see the empty spaces

These aren't actually empty spaces. Well, they are, but not really. :-) What I mean is that they are not placeholders representing individual "empty" items. That's just what you see when the entire combo box is empty (contains no items). Because it contains no items, it can't auto-size the height of its drop-down window, so it uses a fixed size.

I had same problem with ComboBox. Noticed that error occurs only if user hits somewhere else after removing the item but not selecting a new one. Solved it by adding selection of new item after removal. Also handled last item as special case. See working code below:

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
    ComboBox comboBox = sender as ComboBox;

    switch (e.KeyCode)
    {
        case Keys.Delete:
            if ((comboBox.DroppedDown) && (comboBox.SelectedItem != null))
            {
                if (comboBox.Items.Count == 1)  // Removing Last Item
                {
                    comboBox.DroppedDown = false;
                    comboBox.Text = string.Empty;
                    comboBox.Items.Clear();
                }
                else
                {
                    comboBox.Items.Remove(comboBox.SelectedItem);
                    comboBox.SelectedIndex = comboBox.Items.Count - 1;
                }
                e.Handled = true;
            }
            break;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM