How can I hide and show a Tab from a TabControl?

3

I am using a TabControl within a form designed in C #, this TabControl has two Tabs and I need to be able to hide one and / or display it depending on a status that arrives as a parameter from another form. I can not find a property of type Visible or in any case Enabled . Is there any way to hide a tab inside a TabControl or at least disable it? Thank you very much

    
asked by Juan Manuel Palacios 23.04.2016 в 00:46
source

4 answers

3

The winfoms tabs do not have property Visible , you must remove it, if I remember correctly it is done by:

TabControl.TabPages.Remove(TabPage);

Edit:

Here is an example on the microsoft website

    
answered by 23.04.2016 / 00:50
source
2

You can occupy the parent property, where you assign a null parent to hide the tabitem

EsteTabItem.Parent = null;

And to show you assign the control tab in which you will find

EsteTabItem.Parent = TbCtrlGeneral;
    
answered by 20.05.2016 в 02:26
1

In principle, the Windows Forms TabControl does not allow you to show / hide a tab. What you can do is eliminate it even if you lose the controls on that tab.

One way to do this would be to store TabPage-type controls that you have marked as invisible in an array in order to be able to add them again when you make them visible.

I hope it does not bother you to write the code of a control that does just this, it descends from TabControl and keeps a list with the invisible controls to show them later. The other array (arrBoolPagesVisible) is used to maintain the indexes and insert the controls in the same place they were at the beginning.

Passing the index of the tab you want to show / hide to the ShowTab method of this control, you will get the result you are looking for.

using System;
using System.Collections.Generic;

namespace Bau.Controls.TabControls
{
/// <summary>
///     Control que extiende <see cref="TabControl"/> para permitir ocultar fichas
/// </summary>
public class TabControlExtended : System.Windows.Forms.TabControl
{ // Variables con las páginas
        private List<System.Windows.Forms.TabPage> objColPages = null;
        private bool[] arrBoolPagesVisible;

    /// <summary>
    ///     Inicializa las variables antes de procesar
    /// </summary>  
    private void InitControl()
    { if (objColPages == null)
            { // Inicializa la colección de páginas y elementos visibles
                    objColPages = new List<System.Windows.Forms.TabPage>();
                    arrBoolPagesVisible = new bool[TabPages.Count];
                // Añade las páginas de la ficha a la colección e indica que son visibles
                    for (int intIndex = 0; intIndex < TabPages.Count; intIndex++)
                        { // Añade la página
                                objColPages.Add(TabPages[intIndex]);
                            // Indica que es visible
                                arrBoolPagesVisible[intIndex] = true;
                        }
            }
    }   

    /// <summary>
    ///     Muestra una ficha
    /// </summary>
    public void ShowTab(int intTab)
    { ShowHideTab(intTab, true);
    }

    /// <summary>
    ///     Oculta una ficha
    /// </summary>
    public void HideTab(int intTab)
    { ShowHideTab(intTab, false);
    }

    /// <summary>
    ///     Muestra / oculta una ficha
    /// </summary>
    public void ShowHideTab(int intTab, bool blnVisible)
    { // Inicializa el control
            InitControl();
        // Oculta la página
            arrBoolPagesVisible[intTab] = blnVisible;
        // Elimina todas las fichas
            TabPages.Clear();
        // Añade únicamente las fichas visibles
            for (int intIndex = 0; intIndex < objColPages.Count; intIndex++)
                if (arrBoolPagesVisible[intIndex])
                    TabPages.Add(objColPages[intIndex]);
    }

    /// <summary>
    ///     Cuenta el número de fichas visibles
    /// </summary>
    public int CountTabsVisible
    { get
            { int intNumber = 0;

                    // Cuenta el número de páginas visibles
                        if (objColPages != null)
                            for (int intIndex = 0; intIndex < arrBoolPagesVisible.Length; intIndex++)
                                if (arrBoolPagesVisible[intIndex])
                                    intNumber++;
                    // Devuelve el número de páginas visibles
                        return intNumber;
            }
    }
  }
}
    
answered by 03.12.2016 в 00:03
-1

An example to remove and add TabPages to tabControl :

if (this.Editar)
{
    this.tabControlDetalle.TabPages.Add(this.tabPageEdicion);
    this.tabControlDetalle.TabPages.Add(this.tabPageListaPersonas);
}
else
{
    this.tabControlDetalle.TabPages.Remove(this.tabPageEdicion);
    this.tabControlDetalle.TabPages.Remove(this.tabPageListaPersonas);
}
    
answered by 25.06.2017 в 00:48