Winforms 中的简单组合框 - 触发两次(使用鼠标展开组合框并使用键盘 select 一个项目)

Simple Combobox in Winforms - Firing twice (Use mouse to expand combobox and select a item using keyboard)

考虑一个带有简单组合框的非常基本的表单

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.comboBox1.Items.Add("test1");
        this.comboBox1.Items.Add("test2");
        this.comboBox1.Items.Add("test3");
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.comboBox1.SelectedIndexChanged += (o, args) => 
        {
            MessageBox.Show("Combo box changed!");
        };
    }
}

我什至将事件处理程序更改为以下代码。 (基于链接的问题。仍然是同样的问题)

this.comboBox1.SelectedValueChanged += (o, args) => //or even `Textchanged` event too
            {

                MessageBox.Show("Combo box changed!");
            };

使用 mouse 和 select 任何使用 keyboard 的项目展开下拉列表。

组合框触发两次(消息框出现两次)

知道为什么吗?

这似乎是底层框架的一个错误。

所以,我设法解决了这个问题。

方法如下:

  1. 有一个私有变量(最好)可以存储组合框中所选项目的值
  2. 下次,将其与组合框中的当前选择进行比较。如果它们相同,则 return.