?
Solved

cancel event SelectedIndexChanged

Posted on 2007-08-09
3
Medium Priority
?
2,648 Views
Last Modified: 2013-11-05
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?
0
Comment
Question by:ANAT2403
  • 2
3 Comments
 
LVL 12

Expert Comment

by:RubenvdLinden
ID: 19661384
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
 

Author Comment

by:ANAT2403
ID: 19661669
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
 
LVL 12

Accepted Solution

by:
RubenvdLinden earned 2000 total points
ID: 19661805
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question