views:

209

answers:

3

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.

A: 

Can you check the index of the active tab and workaround using that?

Richard Ev
A: 

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.

Brody
A: 

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.

Hans Passant
I fixed the problem in this way. I actually call a function to switch the tabs and in there I set a string with the name of the current tab before the switch. The tabPage_Enter method checks to see if it is already up and so I don't run the code. Thanks nobuz you pointed me the right way.
Matt