为什么设置 comboBox.SelectedIndex 无法更改 ComboBox 可编辑部分中显示的内容?

Why does setting comboBox.SelectedIndex fail to change what appears in the editable portion of my ComboBox?

我有一个 C# Winform 应用程序。它有一个ComboBox。我的目标是在键入 delete 键时,让 ComboBox 下拉列表中选择的项目出现在 ComboBox 的可编辑部分。例如,如果 ComboBox 有项目 AB。和 C,然后在加载 Form 时显示项目 A。如果我单击下拉列表,将鼠标悬停在项目 C 上,然后键入 delete 键,我希望关闭下拉列表并且 C 出现在ComboBox.

实际上,我已经验证我正在获取所选项目的文本,但是代码行 comboBox.SelectedIndex = comboBox.FindStringExact(selectedItemText); 不会更改 ComboBox[= 的可编辑部分中显示的内容30=]

MCVE:

注意:表单有一个名为 combobox 的组合框和一个名为 textbox

的文本框
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Forms;

namespace Winforms_Scratch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //using string collection because I need to simulate what is returned from an Application Settings list
            StringCollection computerList = new StringCollection { "C", "B", "A" };

            ArrayList.Adapter(computerList).Sort();

            comboBox.DataSource = computerList;

            comboBox.KeyDown += ComboBox_KeyDown;

            computerList = null;

        }

        private void ComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && comboBox.DroppedDown)
            {
                ComboBox comboBox = (ComboBox)sender;

                //get the text of the item in the dropdown that was selected when the Delete key was pressed
                string selectedItemText = comboBox.GetItemText(comboBox.SelectedItem);

                //take focus away from the combobox to force it to dismiss the dropdown
                this.Focus();

                //load selectedItemText into the textbox just so we can verify what it is
                textBox.Text = selectedItemText;

                //set the comboBox SelectedIndex to the index of the item that matches the  
                //text of the item in the dropdown that was selected when the Delete key was pressed
                comboBox.SelectedIndex = comboBox.FindStringExact(selectedItemText);
                comboBox.Refresh();

                //Stop all further processing
                e.Handled = true;

            }
            else if (e.KeyCode == Keys.Down && !comboBox.DroppedDown)
            {
                //If the down arrow is pressed show the dropdown list from the combobox
                comboBox.DroppedDown = true;
            }
        }


    }
}

我的猜测是组合框在失去焦点后表现不同。无论如何,根据我对您的要求的理解,我进行了以下更改并且它有效。如果有单独的要求 return 焦点到 window,您可以在设置选中项后调用 this.Focus()。您的 SelectedIndex/FindStringExact 方法与将 SelectedItem 设置为字符串的方法相同。

我去掉了文本框,因为据我所知,这只是为了调试目的。

private void ComboBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Delete && comboBox.DroppedDown)
    {
        ComboBox comboBox = (ComboBox)sender;

        //  Get the text of the item in the dropdown that was selected when the 
        //  Delete key was pressed
        string selectedItemText = comboBox.GetItemText(comboBox.SelectedItem);

        comboBox.DroppedDown = false;

        comboBox.SelectedItem = selectedItemText;

        //Stop all further processing
        e.Handled = true;

    }
    else if (e.KeyCode == Keys.Down && !comboBox.DroppedDown)
    {
        //  If the down arrow is pressed show the dropdown list from the combobox
        comboBox.DroppedDown = true;
    }
}