18

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

bluish
  • 26,356
  • 27
  • 122
  • 180
Aan
  • 12,247
  • 36
  • 89
  • 150

10 Answers10

30
  • 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);
    
Community
  • 1
  • 1
Otiel
  • 18,404
  • 16
  • 78
  • 126
  • +1 for giving specs to justify why `.Enabled` & `.Show` aren't applicable. Also for giving info and link to disabling all controls in the tab page. But do you think it would have been better to save a for iteration, if Specs had allowed `.Enabled`? – bonCodigo Jun 29 '14 at 13:42
  • Yeah, the best answer. I'll answer some code based on it. – kokbira Aug 15 '15 at 14:37
  • should be the answer – Jar Oct 29 '18 at 17:00
6

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;
    }
}
Daniel Ballinger
  • 13,187
  • 11
  • 69
  • 96
John S.
  • 1,937
  • 2
  • 18
  • 28
  • I found several quirks with this approach when I tried it. I added the hidden TabPages to a TabControl.TabPageCollection to keep a keyed reference to them. Doing so executed InitLayout() again and changed the _parent. I needed to initialise _index to a value < 0. I needed to check that the _index was less than the size on the `_parent.TabPages`, otherwise I needed to stick with the Add. – Daniel Ballinger Sep 02 '14 at 22:14
5

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.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • 15
    As far as I can see, TabPage doesn't have properties Enabled and Visible – Nikola Davidovic Oct 15 '12 at 12:03
  • Mine does - as I compiled and ran that. – BugFinder Oct 15 '12 at 12:04
  • 9
    In Framework 4, C# winforms there is no such property, I've checked it, believe me. Maybe in ASP.NET? – Nikola Davidovic Oct 15 '12 at 12:09
  • 3
    @BugFinder: I wonder which .NET Framework version you are using. According to [msdn](http://msdn.microsoft.com/en-us/library/system.windows.forms.tabpage_properties.aspx), the `Enabled` and the `Visible` properties are: "*Infrastructure. This member is not meaningful for this control.*" – Otiel Oct 15 '12 at 12:09
  • I have VS2010 pro installed. Odd then it compiled and worked for me. I didnt think twice of it - I have no extensions loaded either. – BugFinder Oct 15 '12 at 12:10
  • Maybe when you write tabPage1 you are referring to the TabControl not TabPage? – Nikola Davidovic Oct 15 '12 at 12:11
  • Nope.. definately tabPage.. TabControl is the housing for the 2 pages. – BugFinder Oct 15 '12 at 12:12
  • @BugFinder: It works with me in MS VS 2010.. thanks. – Aan Oct 15 '12 at 12:18
  • 9
    Just tested myself (VS 2010/.NET 4.0), and it builds without error also. But the properties are not proposed by Intellisense (weird). The `Enabled` property works, but not the `Visible` one. – Otiel Oct 15 '12 at 12:19
  • Visible hides the controls on the tab, rather than hides the tab - as described .. its a bit odd – BugFinder Oct 15 '12 at 12:19
  • @BugFinder: Not with me. `tabPage.Visible = false;` does not have any effect. – Otiel Oct 15 '12 at 12:25
  • In msdn `This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.` What does that mean? – Aan Oct 15 '12 at 12:29
  • Otiel odd, I put controls on my tab page, and the visible hide them all and showed them all. – BugFinder Oct 15 '12 at 13:45
  • Well, it does not perform what author of the question wanted. – kokbira Aug 15 '15 at 14:34
5
//Move&Add is not good answer   
this.tabPage1.Parent = null; // hide    
this.tabPage1.Parent = this.tabControl1; //show
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
user3304385
  • 51
  • 1
  • 1
3

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).

Nikola Davidovic
  • 8,556
  • 1
  • 27
  • 33
2

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;
     }
sindhu jampani
  • 504
  • 3
  • 10
  • 30
  • @Sindhu..is it not possible to achieve it directly..? – Sai Avinash Mar 10 '14 at 09:12
  • 1
    @Avinash Kothamasu..If you want to Hide the tabpage without hiding its header,then you can do like this. private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) { //To Disable Tab 4 if (tabControl1.SelectedTab == tabPage4) { tabControl1.SelectedTab = tabPage1; } }If you want to hide the tabPage along with its header,then you should follow the previously given code. – sindhu jampani Mar 10 '14 at 12:19
  • @Sindhu..thank for explanation. I got it now.. – Sai Avinash Mar 11 '14 at 05:59
1
// 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;
OhBeWise
  • 5,350
  • 3
  • 32
  • 60
ISB
  • 112
  • 7
0

What about tabPage.Enabled and tabPage.Visible properties?

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

Gianni B.
  • 2,691
  • 18
  • 31
0

Put tabpage into panel and hide panel using

this.panel1.visible=false;

It is working for me !

Suraj Shrestha
  • 1,790
  • 1
  • 25
  • 51
THE INN-VISIBLE
  • 155
  • 1
  • 8
0

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
}
kokbira
  • 608
  • 1
  • 10
  • 25