Cedric Mamo
Cedric Mamo

Reputation: 1744

Winforms ComboBox only fire event when item has changed

I have a ComboBox with several items. I have added an event handler to the SelectedIndexChanged event. Suppose, the list has two items, A and B. If the currently selected item is A and the user changes it to B then the event is fired and that's OK. However, if the user clicks on the ComboBox and clicks on A again (meaning the item has not actually changed) the event is still fired. I would like for the event to only be fired if the item has definitely changed, or something which will allow me to accomplish this.

Upvotes: 0

Views: 19935

Answers (7)

Raymond Dumalaog
Raymond Dumalaog

Reputation: 353

in your InitializeComponent(), put this in your combobox

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

Then in your main method create the combobox method like

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";
        }
}

Upvotes: 0

Mr_Green
Mr_Green

Reputation: 41832

I think this is what you mean:

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

Upvotes: 2

acrilige
acrilige

Reputation: 2456

Better to incapsulate this logic in class derived from ComboBox (ComboBoxEx in my example)

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;
                }
        }
    }

And use it like this:

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");
    }

Upvotes: 3

Cedric Mamo
Cedric Mamo

Reputation: 1744

Simply adding the event handler to the TextChanged event instead of SelectedIndexChanged has solved my problem. This works in my case because I can be sure that no two items in the ComboBox (which is a dropdown list) will have the same text.

Upvotes: 2

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

If you don't plan change ComboBox selection diagrammatically try use SelectionChangeComitted.

Upvotes: 5

jAC
jAC

Reputation: 5324

You can define a int variable with the SelectedIndex of your ComboBox, then you can check if the variable has the same value as the index. If yes, don't do anything, else do the stuff.

int lastIndex = myComboBox.SelectedIndex;

Then in the SelectedIndexChangedEvent:

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

Upvotes: 1

Maarten
Maarten

Reputation: 22945

One solution would be to databind the combobox to a property in a model class. The model should then implement the interface INotifyPropertyChanged (and firing it correctly, so only when a value is changed). You can then use the PropertyChanged event to handle a change in the selection of the control, and this event would only be fired when an actual change happened.

Upvotes: 1

Related Questions