Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
Programmatically selecting an existing TabPage in a TabControl shows a blank page

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.

Tabcontrol empty page

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If you create a new TabPage with the provided text (as in TabPage tp = new(TabCaption);), your tp object is not the same as an existing TabPage with the same Caption, so TabManager.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:

private void AddControls(Control uc, string tabCaption)
{
    if (TabManager.TabCount == 0 || TabManager.TabPages.IndexOfKey(tabCaption) < 0) {
        var tp = new TabPage(tabCaption);
        // or TabPage tp = new(tabCaption);
        tp.Name = tabCaption;
        uc.Dock = DockStyle.Fill;
        tp.Controls.Add(uc);
        TabManager.TabPages.Add(tp);
            
    }
    TabManager.SelectedTab = TabManager.TabPages[tabCaption];
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!