Winforms ComboBox仅在项目更改时触发事件

时间:2012-11-27 10:26:04

标签: c# .net winforms

我有ComboBox个项目。我在SelectedIndexChanged事件中添加了一个事件处理程序。假设,列表有两个项目,A和B.如果当前选择的项目是A,并且用户将其更改为B,则会触发该事件,这是正常的。但是,如果用户点击ComboBox并再次点击A(意味着该项尚未实际更改),则仍会触发该事件。我希望事件只有在项目确实发生变化时才会被解雇,或者是允许我完成此任务的事情。

7 个答案:

答案 0 :(得分:4)

如果您没有按计划选择更改ComboBox选择,请尝试使用SelectionChangeComitted

答案 1 :(得分:3)

最好在从ComboBox派生的类中填充此逻辑(在我的示例中为ComboBoxEx)

private class ComboBoxEx : System.Windows.Forms.ComboBox
    {
        Int32 _lastIndex = -1;
        protected override void OnSelectedIndexChanged(System.EventArgs e)
        {
            if (_lastIndex == -1)
            {
                _lastIndex = this.SelectedIndex;
                base.OnSelectedIndexChanged(e);
            }
            else
                if (_lastIndex != this.SelectedIndex)
                {
                    base.OnSelectedIndexChanged(e);
                    _lastIndex = this.SelectedIndex;
                }
        }
    }

并像这样使用它:

public Form1()
    {
        var combobox = new ComboBoxEx() { DropDownStyle = ComboBoxStyle.DropDownList };
        combobox.Items.Add("Item 1");
        combobox.Items.Add("Item 2");
        combobox.Items.Add("Item 3");
        this.Controls.Add(combobox);
        combobox.SelectedIndexChanged += OnIndexChanged;

        InitializeComponent();
    }

    private void OnIndexChanged(object sender, EventArgs e)
    {
        MessageBox.Show("Index changed");
    }

答案 2 :(得分:2)

我认为这就是你的意思:

int intIndex; //Global Variable
//In your ComboBox_SelectedIndex Changed event
if(myComboBox.SelectedIndex != intIndex) 
{
   //your code
   intIndex = myComboBox.SelectedIndex;
}

答案 3 :(得分:2)

简单地将事件处理程序添加到TextChanged事件而不是SelectedIndexChanged已解决了我的问题。这适用于我的情况,因为我可以确定ComboBox中没有两个项目(这是一个下拉列表)将具有相同的文本。

答案 4 :(得分:1)

一种解决方案是将组合框数据绑定到模型类中的属性。然后,该模型应实现接口INotifyPropertyChanged(并正确触发它,因此仅在更改值时)。然后,您可以使用PropertyChanged事件来处理控件选择的更改,并且只有在发生实际更改时才会触发此事件。

答案 5 :(得分:1)

您可以使用ComboBox的SelectedIndex定义int变量,然后可以检查变量是否与索引具有相同的值。 如果是的话,不要做任何事,否则就做。

int lastIndex = myComboBox.SelectedIndex;

然后在SelectedIndexChangedEvent:

if(lastIndex != myComboBox.SelectedIndex){
     //do something
}

答案 6 :(得分:0)

在你的 InitializeComponent()

,将它放在你的组合框中

private void InitializeComponent()
{
   ///combobox
   ///This line will triger if the combobox has changed
   this.combobox.SelectedIndexChanged += System.EventHandler(this.comboboxChanged);
}

然后在你的main方法中创建像

这样的组合框方法
private string previousValue;

Private void comboboxChanged(object sender, EventArgs e)
{
    if (combobox.Text == "A" && previousValue == "A")
        {
            //Do nothing
            previousValue = "A";
        }
    else if (combobox.Text == "B" && previousValue == "B")
        {
            //Do Nothing
            previousValue = "B";
        }
    else if (combobox.Text == "A")
        {
            //Do Something
            previousValue = "A";
        }
    else if (combobox.Text == "B")
        {
            //Do Something
            previousValue = "B";
        }
}
相关问题