I use this method to create a new TabPage in a TabControl (TabManager) if a TabPage with the specified text doesn't exist, or just select it if it already exists:
private void AddControls(UserControl uc, string TabCaption)
{
Boolean TabFound = false;
if (TabManager.TabCount == 0)
{
TabPage tp = new(TabCaption);
TabManager.TabPages.Add(tp);
uc.Dock = DockStyle.Fill;
tp.Controls.Add(uc);
TabManager.SelectedTab = tp;
}
else
{
TabPage tp = new(TabCaption);
foreach (TabPage tp1 in TabManager.TabPages)
{
if (tp1.Text == TabCaption)
{
TabFound = true;
}
}
if (TabFound != true)
{
TabManager.TabPages.Add(tp);
uc.Dock = DockStyle.Fill;
tp.Controls.Add(uc);
TabManager.SelectTab(tp);
//tp.Show();
//tp.BringToFront();
}
else
{
TabManager.SelectedTab = TabManager.TabPages[tp.Name];
return;
}
}
}
The problem is that the TabPage is not selected, instead an empty page is shown.
The offending code appears to be:
TabManager.SelectedTab = TabManager.TabPages[tp.Name];
As it will only show an empty TabPage.
I searched for documentation but found no solution so far.

If you create a new TabPage with the provided text (as in
TabPage tp = new(TabCaption);), yourtpobject is not the same as an existing TabPage with the same Caption, soTabManager.SelectTab(tp);won't select it (not the same object).You see a blank background because when you use the TabControl.SelectedTab() method and the TabPage specified doesn't exist, no TabPage is the Current, so you just see the TabControl background.
To determine whether to add or just select a TabPage, you can check whether the TabControl has no TabPages (as you're doing) and also verify whether a TabPage with the same Name already exists.
You can use the TabPageCollection.IndexOfKey() method to perform this check.
You should assign a Name to your new TabPage, not just a Caption, as it happens when you create a new TabPage in the Designer.
This simplifies the creation and/or selection of TabPages. Your code could then be: