0
votes

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.

1

1 Answers

1
votes

Why is the tab page body not updating with a .NET tab control?

So after searching for over 3 days I stumbled upon this by clicking through layers of related questions.

It looks like the timing of changing the selected tab while the tabcontrol is still processing events messes it up to the point it stops firing events (in my case). Substituting the last line of code with this.BeginInvoke(new Action(() => tabControl_Buizen.SelectedIndex -= 1)); has solved my problem.