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?
ANAT2403Asked:
Who is Participating?
 
RubenvdLindenConnect With a Mentor Commented:
Almost the same solution:


public partial class Form1 : Form
    {
        int oldIndex = -1;
        bool askConfirmation = true;
       
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                if (askConfirmation)
                {
                     if (! yourCondition)
                     {
                          askConfirmation = false;
                          comboBox1.SelectedIndex = oldIndex;
                     }

                     else
                     {
                           oldIndex = comboBox1.SelectedIndex;
                     }
                }

                else
                {
                      oldIndex = comboBox1.SelectedIndex;
                      askConfirmation = true;
                }
        }  
    }


I hope this works better for you.
0
 
RubenvdLindenCommented:
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;
                     }
                }
        }  
    }
0
 
ANAT2403Author Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.