tags:

views:

1468

answers:

2

in my comboBox2_SelectedIndexChanged event i do some processing and in certain cases i want to change the SelectedIndex back to what it was before i change it. I see that sender.selectedIndex is the last index i used however i cant access that var. How do i tell the combobox to go back to w/e it was?

+1  A: 

See the code below. If the case satisfies you set the selected index to the lastIndex, while setting lastIndex, you make sure to unsubscribe the selection event, set and resubscribe. Track the lastIndex each time by setting it to the selectedIndex.

    int lastIndex=0;
    void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
    {            
        if (isItTheCase) 
        {
            comboBox2.SelectedIndexChanged -= new EventHandler(comboBox2_SelectedIndexChanged);
            comboBox2.SelectedIndex = lastIndex;
            comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
        }
        lastIndex = comboBox2.SelectedIndex;
    }
nils_gate
A: 

Please check anywhere you are registering the SelectedIndexChanged more than once. It generally happens that you register events at design time which goes in Initilizecomponent() method and then register it in the constructor.

Thanks