none
Why can't I hide a tab page? RRS feed

  • Question

  • In my Windows Form I have a tab control with two tab pages. In form load event handler I have:


                tc1.TabIndex = ix;
                TabPage tp = tc1.TabPages[ix];

                tp.Show();

                for (int i = 0; i < tc1.TabPages.Count; i++)
                {
                    if (i != ix)
                        tc1.TabPages[i].Hide();
                }

    But both tab pages still show. Why can't I hide one?

    Thank you,


    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    • Moved by CoolDadTx Thursday, May 28, 2020 1:40 PM Winforms related
    Monday, May 18, 2020 9:37 PM

Answers

  • Hi Jon Q Jacobs,

    Thank you for posting here.

    Unfortunately, we cannot "hide" it.

    If it is not needed, we can remove it.

    tabControl1.TabPages.Remove(tabPage1);

    tabControl1.TabPages.RemoveAt(0);

    If you want to add it back, you can use the "Add" or "Insert" method.

        tabControl1.TabPages.Add(tabPage1);
        tabControl1.TabPages.Insert(0,"tabPage3");

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jon Q Jacobs Thursday, May 28, 2020 12:14 PM
    Thursday, May 28, 2020 6:33 AM

All replies

  • Hi Jon Q Jacobs,

    Thank you for posting here.

    Unfortunately, we cannot "hide" it.

    If it is not needed, we can remove it.

    tabControl1.TabPages.Remove(tabPage1);

    tabControl1.TabPages.RemoveAt(0);

    If you want to add it back, you can use the "Add" or "Insert" method.

        tabControl1.TabPages.Add(tabPage1);
        tabControl1.TabPages.Insert(0,"tabPage3");

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jon Q Jacobs Thursday, May 28, 2020 12:14 PM
    Thursday, May 28, 2020 6:33 AM
  • You can use the following class which came from this post. I made several small changes from the original.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace TabsHelper
    {
        public class TabControlHelper
        {
            private TabControl _tabControl;
            private List<KeyValuePair<TabPage, int>> _pagesIndexed;
    
            public TabControlHelper()
            {
                
            }
            public TabControlHelper(TabControl tabControl)
            {
                _tabControl = tabControl;
                _pagesIndexed = new List<KeyValuePair<TabPage, int>>();
    
                for (int index = 0; index < tabControl.TabPages.Count; index++)
                {
                    _pagesIndexed.Add(
                        new KeyValuePair<TabPage, int>(
                            tabControl.TabPages[index], index));
                }
            }
    
            public void HideAllPages()
            {
                for (int index = 0; index < _pagesIndexed.Count; index++)
                {
                    _tabControl.TabPages.Remove(_pagesIndexed[index].Key);
                }
            }
    
            public void ShowAllPages()
            {
                for (int index = 0; index < _pagesIndexed.Count; index++)
                {
                    _tabControl.TabPages.Add(_pagesIndexed[index].Key);
                }
            }
    
            public void HidePage(TabPage tabPage)
            {
                if (!_tabControl.TabPages.Contains(tabPage)) return;
                _tabControl.TabPages.Remove(tabPage);
            }
    
            public void ShowPage(TabPage tabPage)
            {
                if (_tabControl.TabPages.Contains(tabPage)) return;
                InsertTabPage(GetTabPage(tabPage).Key, GetTabPage(tabPage).Value);
            }
    
            private void InsertTabPage(TabPage tabPage, int index)
            {
                if (index < 0 || index > _tabControl.TabCount)
                    throw new ArgumentException("Index out of Range.");
    
                _tabControl.TabPages.Add(tabPage);
    
                if (index < _tabControl.TabCount - 1)
                    do
                    {
                        SwapTabPages(tabPage, 
                            (_tabControl.TabPages[_tabControl.TabPages.IndexOf(tabPage) - 1]));
                    }
                    while (_tabControl.TabPages.IndexOf(tabPage) != index);
    
                _tabControl.SelectedTab = tabPage;
    
            }
    
            private void SwapTabPages(TabPage tabPage1, TabPage tabPage2)
            {
                if (_tabControl.TabPages.Contains(tabPage1) == false || _tabControl.TabPages.Contains(tabPage2) == false)
                    throw new ArgumentException("TabPages must be in the TabControls TabPageCollection.");
    
                int index1 = _tabControl.TabPages.IndexOf(tabPage1);
                int index2 = _tabControl.TabPages.IndexOf(tabPage2);
                _tabControl.TabPages[index1] = tabPage2;
                _tabControl.TabPages[index2] = tabPage1;
            }
    
            private KeyValuePair<TabPage, int> GetTabPage(TabPage tabPage)
            {
                return _pagesIndexed.First(p => p.Key == tabPage);
            }
        }
    }

    Usage

    using System.Windows.Forms;
    
    namespace TabsHelper
    {
        public partial class Form1 : Form
        {
            private TabControlHelper _tabControlHelper = new TabControlHelper();
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
            private void Form1_Shown(object sender, System.EventArgs e)
            {
                _tabControlHelper = new TabControlHelper(tabControl1);
            }
            private void HideAllButton_Click(object sender, System.EventArgs e) => _tabControlHelper.HideAllPages();
            private void ShowAllButton_Click(object sender, System.EventArgs e) => _tabControlHelper.ShowAllPages();
            private void HideTab2Button_Click(object sender, System.EventArgs e) => _tabControlHelper.HidePage(tabPage2);
            private void ShowPage2Button_Click(object sender, System.EventArgs e) => _tabControlHelper.ShowPage(tabPage2);
        }
    }
    

    Or if using an older version of C#

    using System.Windows.Forms;
    
    namespace TabsHelper
    {
        public partial class Form1 : Form
        {
            private TabControlHelper _tabControlHelper = new TabControlHelper();
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
            private void Form1_Shown(object sender, System.EventArgs e)
            {
                _tabControlHelper = new TabControlHelper(tabControl1);
            }
            private void HideAllButton_Click(object sender, System.EventArgs e)
            {
                _tabControlHelper.HideAllPages();
            }
            private void ShowAllButton_Click(object sender, System.EventArgs e)
            {
                _tabControlHelper.ShowAllPages();
               
            }
            private void HideTab2Button_Click(object sender, System.EventArgs e)
            {
                _tabControlHelper.HidePage(tabPage2);
            }
            private void ShowPage2Button_Click(object sender, System.EventArgs e)
            {
                _tabControlHelper.ShowPage(tabPage2);
            }
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, May 28, 2020 11:32 AM
    Moderator
  • Thank you, Timon. You nailed it. Thank you, Kareninstructor for your detailed response. After posting 10 days ago, I came up with pretty much the same solution, although not quite as elegant. You both have confirmed what I came to suspect. "Hide" didn't work and the only effective hiding was removal from the tab control.

    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't


    Thursday, May 28, 2020 12:18 PM
  • You can hide Tabs with TabControl Class

     (ElementHost to add it)

    Test =>

    Thursday, May 28, 2020 12:36 PM
  • Unfortunately that refers to WPF, which this application is not. Thank you, though.

    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    Thursday, May 28, 2020 1:44 PM
  • Well, you knew that. Thank you for the ElementHost reference. I never imagined such a class existed. There are a number of capabilities I have seen in WPF that should have been in Windows Forms controls. I will study more about that class and add it to my repertoire.

    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    Thursday, May 28, 2020 1:49 PM
  • Unfortunately that refers to WPF, which this application is not. Thank you, though.

    I tested in Winforms

    Embedding WPF controls is like embedding any ActiveX control...

    Thursday, May 28, 2020 1:50 PM
  • Yes. I looked back at the other link and replied accordingly. I guess our posts crossed paths in the ether. Thank you again.

    Jon Jacobs, There are 10 kinds of people: those who understand binary and those who don't

    Thursday, May 28, 2020 1:55 PM