Question

How I can make a TabPage in a TabControl visible/hidden and enabled/disabled?

Was it helpful?

Solution

You maybe missing the obvious because neither of the following removes/changes the look of the tab

        tabPage1.Enabled = false; // this disables the controls on it
        tabPage1.Visible = false; // this hides the controls on it.

Neither remove the tab from the list at the top.

OTHER TIPS

  • Enable / disable

    The tabPage.Enabled seems to be working fine, but is marked as "not to be used":

    This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
    This member is not meaningful for this control.

    So you should disable the tab page by disabling every control in the tab. See this for instance.

  • Show / hide

    There is an existing tabPage.Visible property but it does not seem to have any effect. Besides, it is also marked as "not to be used", and msdn advises to remove the tab page from the tab control in order to hide it:

    // Hide the tab page
    tabControl.TabPages.Remove(tabPage1);
    // Show the tab page (insert it to the correct position)
    tabControl.TabPages.Insert(0, tabPage1);
    

I also had this question. tabPage.Visible is not implemented as stated earlier, which was a great help (+1). I found you can override the control and this will work. A bit of necroposting, but I thought to post my solution here for others...

    [System.ComponentModel.DesignerCategory("Code")]
public class MyTabPage : TabPage
{
    private TabControl _parent;
    private bool _isVisible;
    private int _index = int.MinValue;
    public new bool Visible
    {
        get { return _isVisible; }
        set
        {
            if (_parent == null) _parent = this.Parent as TabControl;
            if (_parent == null) return;

            if (_index < 0) _index = _parent.TabPages.IndexOf(this);
            if (value && !_parent.TabPages.Contains(this))
            {
                if (_index > 0 && _index < _parent.TabPages.Count) _parent.TabPages.Insert(_index, this);
                else _parent.TabPages.Add(this);
            }
            else if (!value && _parent.TabPages.Contains(this)) _parent.TabPages.Remove(this);

            _isVisible = value;
            base.Visible = value;
        }
    }

    protected override void InitLayout()
    {
        base.InitLayout();
        _parent = Parent as TabControl;
    }
}
//Move&Add is not good answer   
this.tabPage1.Parent = null; // hide    
this.tabPage1.Parent = this.tabControl1; //show

I don't know about enable/disable (maybe try to disable all the controls on it). If you want them hidden, just remove them from the Items collection. If you want them visible again, you can add them back to the control again. Nevertheless, you will have to take care about their order (store their references in some list, or you can have two lists which hold references to those TabPages that are visible and those that aren't).

we can Enable or disable tab pages by using TABPAGE.ENABLE=true and TABPAGE.ENABLE=FALSE.

but visible property cannot be applied to tabpages.we can instead of visible property,we can do like this.

 private void HideTabPage(TabPage tp)
 {
 if (tabControl1.TabPages.Contains(tp))
 tabControl1.TabPages.Remove(tp);
 }

private void ShowTabPage(TabPage tp)
{
 ShowTabPage(tp, tabControl1.TabPages.Count);
 }

private void ShowTabPage(TabPage tp , int index)
{
 if (tabControl1.TabPages.Contains(tp)) return;
 InsertTabPage(tp, index);
}

 private void InsertTabPage(TabPage tabpage, int index)
{
   if (index < 0 || index > tabControl1.TabCount)
  throw new ArgumentException("Index out of Range.");
   tabControl1.TabPages.Add(tabpage);
   if (index < tabControl1.TabCount - 1)
   do 
    {
    SwapTabPages(tabpage, (tabControl1.TabPages[tabControl1.TabPages.IndexOf(tabpage) - 1]));
     }
    while (tabControl1.TabPages.IndexOf(tabpage) != index);
    tabControl1.SelectedTab = tabpage;
  }
   private void SwapTabPages(TabPage tp1, TabPage tp2)
     {
    if (tabControl1.TabPages.Contains(tp1) == false ||tabControl1.TabPages.Contains(tp2) == false)
        throw new ArgumentException("TabPages must be in the TabControls TabPageCollection.");

     int Index1 = tabControl1.TabPages.IndexOf(tp1);
     int Index2 = tabControl1.TabPages.IndexOf(tp2);
     tabControl1.TabPages[Index1] = tp2;
     tabControl1.TabPages[Index2] = tp1;
    tabControl1.SelectedIndex = tabControl1.SelectedIndex; 
    string tp1Text, tp2Text;
    tp1Text = tp1.Text;
    tp2Text = tp2.Text;
    tp1.Text=tp2Text;
    tp2.Text=tp1Text;
     }
// Hide TabPage and Remove the Header
this.tabPage1.Hide();
this.tabPage3.Hide();
this.tabPage5.Hide();
tabControl1.TabPages.Remove(tabPage1);
tabControl1.TabPages.Remove(tabPage3);
tabControl1.TabPages.Remove(tabPage5);

// Show TabPage and Visible the Header
tabControl1.TabPages.Insert(0,tabPage1);
tabControl1.TabPages.Insert(2, tabPage3);
tabControl1.TabPages.Insert(4, tabPage5);
this.tabPage1.Show();
this.tabPage3.Show();
this.tabPage5.Show();
tabControl1.SelectedTab = tabPage1;

What about tabPage.Enabled and tabPage.Visible properties?

FYI: http://msdn.microsoft.com/en-us/library/8fb09fh2.aspx

Based on @Otiel's answer, I did those two functions:

To toggle visibility:

bool toggleTabPageVisibility(TabControl tc, TabPage tp)
{
    //if tp is already visible
    if (tc.TabPages.IndexOf(tp) > -1)
    {
        tc.TabPages.Remove(tp);
        //no pages to show, hide tabcontrol
        if(tc.TabCount == 0)
        {
            tc.Visible = false;
        }
        return false;
    }
    //if tp is not visible
    else
    {
        tc.TabPages.Insert(tc.TabCount, tp);
        //guarantee tabcontrol visibility
        tc.Visible = true;
        tc.SelectTab(tp);
        return true;
    }
}

To set visibility:

void setTabPageVisibility(TabControl tc, TabPage tp, bool visibility)
{
    //if tp is not visible and visibility is set to true
    if ((visibility == true) && (tc.TabPages.IndexOf(tp) <= -1))
    {
        tc.TabPages.Insert(tc.TabCount, tp);
        //guarantee tabcontrol visibility
        tc.Visible = true;
        tc.SelectTab(tp);
    }
    //if tp is visible and visibility is set to false
    else if ((visibility == false) && (tc.TabPages.IndexOf(tp) > -1))
    {
        tc.TabPages.Remove(tp);
        //no pages to show, hide tabcontrol
        if(tc.TabCount == 0)
        {
            tc.Visible = false;
        }
    }
    //else do nothing
}

Put tabpage into panel and hide panel using

this.panel1.visible=false;

It is working for me !

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top