WinForms ComboBox SelectedIndexChanged 在键入几个字符后按 Alt+Down 时不会触发 [英] WinForms ComboBox SelectedIndexChanged not firing when typing few chars followed by Alt+Down

查看:19
本文介绍了WinForms ComboBox SelectedIndexChanged 在键入几个字符后按 Alt+Down 时不会触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之

当我在 ComboBox 中键入一个字符时,按 Alt+Down 然后按 Enter 或 Tab,即使 SelectedIndex 值确实改变,SelectedIndexChanged 事件也不会触发!为什么事件没有触发?

When I type a character in a ComboBox, press Alt+Down followed by Enter or Tab, the SelectedIndexChanged event doesn't fire, even though the SelectedIndex value does change! Why doesn't the event fire?

更新如果您键入一个字符,按 Alt+Down,然后键入 Esc,则会出现同样的错误.您会期望 Esc 取消更改.但是,SelectedIndex 确实发生了变化,并且 SelectedIndexChanged 事件不会触发.

Update The same error occurs if you type a character, press Alt+Down and then type Esc. You would expect the Esc to cancel the change. However, the SelectedIndex does change, and the SelectedIndexChanged event doesn't fire.

如果您只输入 Alt+Down,使用箭头键浏览到一个条目,然后输入 Esc 会发生什么?是否应该将所选索引设置回其原始值?

What should happen if you just type Alt+Down, use the arrow keys to browse to an entry, and then type Esc? Should the selected index be set back to its original value?

没那么短

我有一个带有 ComboBox 的 WinForm 应用程序.ComboBox 的 SelectedIndexChanged 事件连接到在 Label 控件中显示 SelectedItem 的事件处理程序.ComboBox 的 Items 集合具有三个值:一"、二"和三".

I have a WinForm application with a ComboBox on it. The ComboBox' SelectedIndexChanged event is wired up to a event handler that shows the SelectedItem in a Label control. The ComboBox' Items collection has three values: "One", "Two", and "Three".

  • 当我用鼠标选择一个项目时,事件会触发.
  • 当我滚动鼠标时,事件会触发.
  • 当我使用 Alt+Down 扩展组合框并使用 Up 和 Down 遍历项目时,事件会触发.
  • 但是...当我输入值的第一个字符时,然后按 Alt+Down,然后按 Enter 或 Tab,该值确实被选中并显示在组合框中,但是事件不会触发.
  • When I select an item with the mouse, the event fires.
  • When I scroll the mouse, the event fires.
  • When I use Alt+Down to expand the combobox and walk through the items with Up and Down, the event fires.
  • But... When I type in the first character of a value, then press Alt+Down, followed by Enter or Tab, the value does get selected and is shown in the combobox, but the event doesn't fire.

我还添加了一个显示 SelectedIndex 的按钮.它显示 SelectedIndex 更改.因此,即使 SelectedIndex 确实发生了变化,SelectedIndexChanged 事件也不会触发!

I've also added a button that shows the SelectedIndex. It shows the SelectedIndex has changed. So even though the SelectedIndex does change, the SelectedIndexChanged event does not fire!

如果我只是输入一个有效的值,比如 One 事件也不会触发,但在这种情况下,单击按钮会显示 SelectedIndex确实没变.所以在这种情况下,行为是正常的.

要重现,请创建一个 Form 并添加一个 ComboBox、一个标签和一个按钮.将以下代码放入 Form1.cs 中:

To reproduce, create a Form and add a ComboBox, a Label and a Button. Place the following code in the Form1.cs:

using System;
using System.Windows.Forms;

namespace ComboBoxSelectedIndexChanged
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comboBox1.Items.AddRange(new object[] {
                "One",
                "Two",
                "Three"
            });
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label1.Text = "Selected index: " + comboBox1.SelectedIndex;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Selected item: " + comboBox1.SelectedItem +
                "
Selected index: " + comboBox1.SelectedIndex);
        }
    }
}

推荐答案

此处适当的 DropDown 属性值是 DropDownList.没有这个问题.

The appropriate DropDown property value here is DropDownList. It doesn't have this problem.

针对将 DropDown 样式设置为 DropDown 的特定问题提出解决方法非常困难.它允许用户键入任意文本,甚至与下拉项之一的完美匹配也不会更改 SelectedIndex.您必须实现 Validating 事件并自己寻找匹配项.DropDownClosed 事件对您的特定场景很有用.但实际上,如果您想要完美匹配,请始终使用 DropDownList.

Coming up with a workaround for your specific problem with the DropDown style set to DropDown is quite difficult. It allows the user type arbitrary text and even a perfect match with one of the dropdown items doesn't change the SelectedIndex. You'd have to implement the Validating event and look for a match yourself. The DropDownClosed event would be good for your specific scenario. But really, always use DropDownList if you want perfect matches.

这篇关于WinForms ComboBox SelectedIndexChanged 在键入几个字符后按 Alt+Down 时不会触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆