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

前端 未结 6 1132
闹比i
闹比i 2021-01-17 11:35

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.

6条回答
  •  感动是毒
    2021-01-17 11:36

    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. =(

提交回复
热议问题