Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

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?  
0
jjacksn
Asked:
jjacksn
1 Solution
 
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
 
jjacksnAuthor Commented:
you have to add some other code to not get stuck in an infinite loop, but yes.  
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now