的WinForms组合框的SelectedIndexChanged键入几个字符之后Alt +向下时,没有发射 [英] WinForms ComboBox SelectedIndexChanged not firing when typing few chars followed by Alt+Down

查看:128
本文介绍了的WinForms组合框的SelectedIndexChanged键入几个字符之后Alt +向下时,没有发射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总之



当我在ComboBox中键入一个字符,请按Alt +向下然后按Enter或Tab,SelectedIndexChanged事件没有按T火,即使SelectedIndex的值的确实的改变!为什么没有事件火?



更新
同样的错误,如果你输入一个字符,请按Alt +向下并出现然后键入Esc键。您所期待的Esc键取消更改。然而,将selectedIndex的确实的变化,SelectedIndexChanged事件不火。



如果你只需要输入Alt +向下,使用中应发生什么情况箭头键来浏览到一个条目,而然后的输入ESC?如果所选的索引被设置回其原始值?






并非如此短



我有上有一个组合框一个WinForm应用程序。组合框'SelectedIndexChanged事件是有线到一个事件处理程序,显示在Label控件的SelectedItem。组合框'项目集合有三个值:一,二和三




  • 当我选择一个项目鼠标,事件触发。

  • 当我滚动鼠标,事件触发。

  • 当我使用Alt +向下扩展组合框和通过与上下,该事件触发的项目走。

  • 可是......当我在一个值的第一个字符类型,然后的按Alt +下来,然后按Enter或Tab,该值也都被选择,并在组合框中显示,但不会触发事件。



我还添加了一个按钮,显示的SelectedIndex。它显示的SelectedIndex的的改变。因此,即使将selectedIndex不会改变,SelectedIndexChanged事件不火!



如果我只需键入像的<$ C $有效值C>一个的事件不火下去,但在这种情况下按钮点击揭示的SelectedIndex确实没有改变。因此,在这种情况下,是正常现象。






要复制,创建一个表单并添加一个ComboBox,一个标签和一个按钮。放置在Form1.cs中下面的代码:

 使用系统;使用System.Windows.Forms的
;

命名空间ComboBoxSelectedIndexChanged
{
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent() ;
comboBox1.Items.AddRange(新的对象[] {
一,
二,
三公
});
}

私人无效comboBox1_SelectedIndexChanged(对象发件人,EventArgs五)
{
label1.Text =选择索引:+ comboBox1.SelectedIndex;
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
MessageBox.Show(选择的项目:+ comboBox1.SelectedItem +
\\\
Selected索引:+ comboBox1.SelectedIndex);
}
}
}


解决方案

下面的下拉合适的属性值的DropDownList。它没有这个问题。



未来与您设置为下拉下拉式的特定问题的解决方法是相当困难的。它允许用户键入任意文本,甚至下拉项不改变的SelectedIndex的一个完美匹配。你必须执行验证事件,并查找匹配自己。该DropDownClosed事件将有利于您的具体方案。不过说真的,如果你想完美的比赛总是用DropDownList的。


In short

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?

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.

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?


Not so short

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".

  • 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.

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!

If I just type in a valid value like One the event doesn't fire either, but in that case a click on the button reveals the SelectedIndex indeed hasn't changed. So in that case the behavior is normal.


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 +
                "\nSelected index: " + comboBox1.SelectedIndex);
        }
    }
}

解决方案

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

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组合框的SelectedIndexChanged键入几个字符之后Alt +向下时,没有发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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