How to cancel a Selected Tab Index Changed

Posted on 2005-03-07
Medium Priority
Last Modified: 2006-11-17
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?  
Question by:jjacksn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

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


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,

Accepted Solution

ariellive earned 2000 total points
ID: 13508835
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;
                        nPrevTab = ((System.Windows.Forms.TabControl)sender).SelectedIndex;

Author Comment

ID: 13509304
you have to add some other code to not get stuck in an infinite loop, but yes.  

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

777 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