In the code given in the question, it is not clear where the
TabPage
is being selected. However, when a
TabPage
is selected either programmatically or through the UI, the
SelectionChanged
event will be fired. As a work around, a public field may be declared in the form as a boolean flag say
bool skipSelectionChangedEvent = false;
Now, in the
tabControl1_SelectionChanged
event handler, skip the execution of the code if the boolean flag is true.
if (skipSelectionChangedEvent) return;
And before selecting another
TabPage
programmatically set
skipSelectionChangedEvent = true
skipSelectionChangedEvent = false
I hope this may be helpful.