Link to home
Start Free TrialLog in
Avatar of ANAT2403
ANAT2403Flag for Israel

asked on

cancel event SelectedIndexChanged

In VS2005 winforms C# I have a combobox . In the selected indexchange event I want to save the old value and not to update to the new value.How do I do it?
Avatar of RubenvdLinden
RubenvdLinden

Create a variable on your form to store the value whenever the SelectedIndexChanged value is successfully raised.
If you want to cancel the SelectedIndexChanged the next time, you can simply revert the value to the one that's stored in the variable.

E.g.:

public partial class Form1 : Form
    {
        int oldIndex = -1;
       
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                if (yourCondition)
                {
                     oldIndex = comboBox1.SelectedIndex;
                }
                else
                {
                     if (oldIndex != -1)
                     {
                          comboBox1.SelectedIndex = oldIndex;
                     }
                }
        }  
    }
Avatar of ANAT2403

ASKER

Hi,
This is not bad exept from one problem:
It enter the selectedindexchanged event for  one emore time and if I ask a question in this
event about the condition, this question is raised twice and I don't like it.
Anat
ASKER CERTIFIED SOLUTION
Avatar of RubenvdLinden
RubenvdLinden

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial