ComboBox SelectionChangeCommitted 事件不适用于自动完成 [英] ComboBox SelectionChangeCommitted event doesn't work with AutoComplete

查看:45
本文介绍了ComboBox SelectionChangeCommitted 事件不适用于自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个简短的程序,重现了我刚刚遇到的问题.这是在带有 .NET 4.0 的 MS Windows 7 下编译的,以防万一.

Here is a short program that reproduces the problem I just encountered. This was compiled under MS Windows 7 with .NET 4.0, just in case that makes a difference.

using System;
using System.Drawing;
using System.Windows.Forms;

// Compile with "csc /target:exe /out:comboboxbug.exe /r:System.dll /r:System.Drawing.dll /r:System.Windows.Forms.dll comboboxbug.cs"
// in a Visual Studio command prompt.

static class Program
{
    [STAThread]
    static void Main()
    {
        //Create a label.
        Label oLabel = new Label();
        oLabel.Location = new Point (10, 10);
        oLabel.Size = new Size (100, 15);
        oLabel.Text = "Combo box bug:";
        
        // Create a combo-box.
        ComboBox oComboBox = new ComboBox();
        oComboBox.Location = new Point (10, 50);
        oComboBox.Size = new Size (150, 21);
        oComboBox.Items.AddRange (new object[]
            { "A", "A B", "A C", "A B C", "A C B", "A B C D", "A C B D" });
        oComboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        oComboBox.AutoCompleteSource = AutoCompleteSource.ListItems;
        oComboBox.SelectionChangeCommitted
            += new EventHandler (comboBox_SelectionChangeCommitted);
        
        // Create a form.
        Form oForm = new Form();
        oForm.Size = new Size (200, 150);
        oForm.Controls.Add (oLabel);
        oForm.Controls.Add (oComboBox);
        
        // Run this form.
        Application.Run (oForm);
    }
    static void comboBox_SelectionChangeCommitted (object sender,
        EventArgs e)
    {
        MessageBox.Show ("SelectionChangeCommitted");
    }
}

单击组合框的文本部分并键入A".您将获得自动完成建议的列表.用鼠标单击其中一个选项.SelectionChangeCommitted 事件不会发生!

Click in the text portion of the combo-box and type "A". You will get a list of autocomplete suggestions. Click one of the selections with your mouse. The SelectionChangeCommitted event doesn't happen!

在不使用自动完成功能的情况下选择菜单项.您将收到一个消息框,显示 SelectionChangeCommitted 事件发生了!

Select a menu-item without using autocomplete. You'll get a message-box showing that the SelectionChangeCommitted event happened!

鉴于用户在两种情况下都更改了选择,是否应该在两种情况下都调用 SelectionChangeCommitted?

Given that the selection was changed by the user in both cases, shouldn't SelectionChangeCommitted be called in both cases?

使用 SelectedIndexChanged 事件不是一个选项,因为对于这个固定示例背后的应用程序,我只希望它在用户进行选择时发生,而不是在以编程方式设置时发生.

Using the SelectedIndexChanged event is not an option, because for the application behind this canned example, I only want it to happen when the user makes a selection, not when it's set programmatically.

EDIT 2020-Oct-28:我发现了另一个没有调用 SelectionChangeCommitted 的情况!甚至不需要设置自动完成即可发生问题!单击以打开组合框,按与组合框项目之一的开头匹配的键,然后按 Tab 键离开.组合框项被选中,但没有调用 SelectionChangeCommitted!我修改后的答案如下.

EDIT 2020-Oct-28: I found another case where SelectionChangeCommitted doesn't get called! Auto-complete doesn't even need to be set for the problem to happen! Click to open the combo-box, press a key that matches the beginning of one of the combo-box items, and then press Tab to leave. The combo-box item gets selected, but SelectionChangeCommitted is not called! My revised answer is below.

推荐答案

使用 SelectedIndexChanged 事件不是一个选项,因为对于这个固定示例背后的应用程序,我只希望它在用户进行选择时发生,而不是在以编程方式设置时发生.

Using the SelectedIndexChanged event is not an option, because for the application behind this canned example, I only want it to happen when the user makes a selection, not when it's set programmatically.

您也可以通过编写一个包装方法来更改暂时禁用您的事件的选择来实现此目的.

You could also accomplish this by writing a wrapper method for changing the selection that temporarily disables your event.

不幸的是,我不知道解决SelectionChangeCommitted 没有针对更一般的情况(例如您不控制ComboBox或如何访问).

Unfortunately I do not know off hand a solution to the issue that SelectionChangeCommitted not being started for the more general case (such as where you don't control the ComboBox or how it is accessed).

我制作了 ComboBox 调用的所有事件的流媒体,似乎没有任何其他事件可以满足您的要求.我能想到的唯一解决方案是挂钩 AutoComplete 触发的事件.困难在于知道这些事件是什么,因为从我的小测试显示,它们似乎不会触发 ComboBox.

I made a streamer of all the events that ComboBox calls, and it doesn't appear that any other event will do what you are looking for. The only solution I can think of would involve hooking into the events that the AutoComplete triggers. The difficulty is knowing what those events are, since they don't appear to trigger off the ComboBox from what my minor testing shows.

这篇关于ComboBox SelectionChangeCommitted 事件不适用于自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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