Turn off calling tabPage_Enter method in C#
-
22-07-2019 - |
Question
When I switch tabs with the following code
tabControl1.SelectTab("MyNextTab");
It calls the tabPage_Enter for the tab it is switching from and the tab it is switching to. I want it to be called for the tab it is switching to, but not the tab it is switching from. How would I turn this off. I do know when it happens so if there was a call I could make that would turn off calling the enter method for that tab I could implement that.
Solution
Yes, I repro if I use a button to change the selected tab. TabControl forces the focus onto itself before it changes SelectedIndex. This appears to have been done to avoid problems with the Validating event. The focus change produces the first Enter event, for the active tab, the tab change then produces the second Enter event.
Knowing this, you could set a helper boolean member, indicating that the first Enter event should be ignored. Be careful to check that the current tab isn't already the one you want to select. In a perfect world, this behavior shouldn't matter. The focus really did move to the active tab first.
OTHER TIPS
Can you check the index of the active tab and workaround using that?
Wrap the code in the event within a check so it only processes when you want it to.
If you can write code to switch it off then you could write code to set a state that prevented the code in the event from running.