Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox SelectedIndexChanged event: how to get the previously selected index?

I have a user control which has a ComboBox and a SelectedIndexChanged event handler. In the event handler, I need to be able to tell what was the previously selected index... can anyone point me in the right direction?

private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e)
{
    // need to get the previously selected index and do some handling here...
    // ... some handler code here ...


    switch (cboTargetMode.SelectedIndex)
    {
        case 1:  // ..... some code here...
            break;
        case 2:  // ..... some code here...
            break;
        case 3:  // ..... some code here...
            break;
        default: // ..... some code here...
            break;
    }
}
like image 704
code4life Avatar asked Jul 13 '10 13:07

code4life


People also ask

How do I find the index of a ComboBox?

However, if you want to get the index of selected items in the combobox widget, then you can use the get() method. The get() method returns an integer of the selected item known as the index of the item.

How do I get the ComboBox selected value?

string strID = MyCombobox2. SelectedValue. ToString();

Which method is used to get the selected item in ComboBox control?

The ComboBox class searches for the specified object by using the IndexOf method.


2 Answers

There is nothing built in, you will need to listen for this event and keep track in an instance variable.

Use -1 as an uninitialized "last index", so on first pass you set it but don't use it. Subsequent passes you use it and set it.

You could always do this using a derived ComboBox class of your own and override OnSelectedIndexChanged and expose a PreviousSelectedIndex property. This way, it wouldn't be tightly coupled to the form. Alternatively, as you can do this using events, its also eligible for implementation as an extender provider.

like image 183
Adam Houldsworth Avatar answered Sep 27 '22 19:09

Adam Houldsworth


I guess you will have to store the current (that will become the previous later on) into a variable so that it is used like a cache or something like so.

private void cboTargetMode_SelectedIndexChanged(object sender, EventArgs e) {
    // need to get the previously selected index and do some handling here...
    // ... some handler code here ...

    // Assuming that the variable PreviousSelectedIndex is declared in the class with value -1.
    if (PreviousSelectedIndex < 0)
        PreviousSelectedIndex = cbo.TargetMode.SelectedIndex;
    else
        // Do some handling here...

    switch (cboTargetMode.SelectedIndex) {
        case 1:  // ..... some code here...
            break;
        case 2:  // ..... some code here...
            break;
        case 3:  // ..... some code here...
            break;
        default: // ..... some code here...
            break;
    }
}

Is this something you have already thought of?

Otherwise, perhaps working with the Control.Validating event? I just can't say whether this event occurs before or after the SelectedIndexChanged event. =(

like image 33
Will Marcouiller Avatar answered Sep 27 '22 19:09

Will Marcouiller