Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda