0

I have some validation logic and a TabControl. I want to prevent the user from switching tabs when the current one contains some error.

I have found some people asking the same and some people gave good answers as you can see here.

TabControl- preventing user from changing the selected tab: MessageBox causing bug

HOWEVER, as Fredrik Hedblad tell, when a MessageBox is shown while the selection is being canceled, the whole thing breaks down. It experiences a strange behavior. The event stops being raised (apparently) and if you minimize and restore the Window, it suddenly raised without interacting!!

Can somebody shed some light on this? Thanks a lot!!!

Here I have set up a Visual Studio solution with the problem. It has the code in the MainWindow.cs. Really simple, but doesn't work. Try to click some tab twice (after the Dialog shows) and you will notice it doesn't show the Dialog again, but if you minimize and restore, the Dialog shows automatically and the tab IS SELECTED!

ZIP File With the Sample solution (Visual Studio 2012

Community
  • 1
  • 1
SuperJMN
  • 13,110
  • 16
  • 86
  • 185

2 Answers2

1

In your validation logic set the other TabItems Visibility property to Collapsed
Either that or IsEnabled to false.

I'm guessing you have a IsDirty kind of property in your validation logic, right? Then you're all clear!

trinaldi
  • 2,872
  • 2
  • 32
  • 37
0

Try using This code it works:

bool isError=true;
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (isError)
            {
                e.Cancel = true;
            }            
        }

Where isError is bool variable set when error occurs on tab page.

lokendra jayaswal
  • 298
  • 1
  • 6
  • 19