I have a form with a tabcontrol and multiple tabpages. Those tabpages can be added dynamically and then create some child controls. The process is to click the last tabpage's header ("+"). That fires a deselecting and selecting event on the tabcontrol, for the current page and the +page respectively. Selecting the +page causes a new page to be inserted at the second to last index (and creating childcontrols) and to select it. This last part again fires the deselecting and selecting events on the tabcontrol, now for the +page and new page respectively.
My problem is that when using ctrl+tab to add a tabpage, the selecting and deselecting events for every second new tabpage are not fired. This causes the new tabpage to be empty of child controls. The weird thing is that it works perfectly when using the mouse, arrow keys, and navigating through existing tabpages in any way. Only creating new pages using ctrl+tab doesn't work correctly.
public FormConstructor()
{
tabControl.SelectedIndex = 0;
tabControl.Selecting += tabControl_Selecting;
tabControl.Deselecting += tabControl_Deselecting;
}
private void tabControl_Deselecting(object sender, TabControlCancelEventArgs e)
{
//Code removes events from and disposes of childcontrols - works correctly
}
private void tabControl_Buizen_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPage != tabPage_plus) e.TabPage.Controls.Add(new TextBox()); //create controls
if (e.TabPage == tabPage_plus)
{
tabControl_Buizen.TabPages.Insert(e.TabPageIndex, "Buis " + (e.TabPageIndex + 1).ToString()); //create new tabpage
tabControl_Buizen.SelectedIndex -= 1; //Commenting this out and selecting manually works
}
}
I have found that not selecting the newly created page automatically, and instead navigating backwards using ctrl+shift+tab works fine, but I have not been able to find out why. I have taken a look at the SelectedIndex property, but do not know if this is the cause.
Does anyone have an idea what causes this behaviour of events not being fired? I can add more code if needed.