首页 > 解决方案 > 为什么设置 comboBox.SelectedIndex 无法更改 ComboBox 的可编辑部分中显示的内容?

问题描述

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

事实上,我已经验证我得到了选定的项目文本,但是代码行comboBox.SelectedIndex = comboBox.FindStringExact(selectedItemText);并没有改变显示在可编辑部分的内容ComboBox

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


    }
}

标签: c#winformscomboboxkeydown

解决方案


我的猜测是组合框失去焦点后的行为会有所不同。无论如何,根据我对您要求的理解,我进行了以下更改并且它有效。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;
    }
}

推荐阅读