Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda