How to cancel a Selected Tab Index Changed

I have a panel inside of a Tab, and the data may or may not valid based on the user input.  When a user tries to click onto a different tab, I want to check to make sure ValidateData() is true before allowing them to switch tabs.  Is this possible?  Can I somehow cancel the tab changing event?  
LVL 5
jjacksnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RealMrTeaCommented:
This would be simular to something I asked a while back and you could do simular type thing.  Check this out:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_20906014.html

Just disable all the other tabs until you have validated and then you can enable them again to allow the change...or something more custom could be done.

Hope that helps,
Eric
0
arielliveCommented:
Add this event to the tabControl:
      tabControl.SelectedIndexChanged += new System.EventHandler(OnSelectedIndexChanged);

And this code:
            private int nPrevTab = -1;
            private void OnSelectedIndexChanged(object sender, System.EventArgs e)
            {
                  if (!ValidateData() && nPrevTab != -1)
                        ((System.Windows.Forms.TabControl)sender).SelectedIndex = nPrevTab;
                  else
                        nPrevTab = ((System.Windows.Forms.TabControl)sender).SelectedIndex;
            }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jjacksnAuthor Commented:
you have to add some other code to not get stuck in an infinite loop, but yes.  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

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.