How to hide TabPage from TabControl [duplicate]

2019-01-03 05:53发布

This question already has an answer here:

How to hide TabPage from TabControl in WinForms 2.0?

22条回答
ら.Afraid
2楼-- · 2019-01-03 06:26

Well, if you don't want to mess up existing code and just want to hide a tab, you could modify the compiler generated code to comment the line which adds the tab to the tabcontrol.

For example: The following line adds a tab named "readformatcardpage" to a Tabcontrol named "tabcontrol"

this.tabcontrol.Controls.Add(this.readformatcardpage);

The following will prevent addition of the tab to the tabcontrol

//this.tabcontrol.Controls.Add(this.readformatcardpage);

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-03 06:28

Create a new empty class and past this inside it:

using System.Windows.Forms;

namespace ExtensionMethods
{
    public static class TabPageExtensions
    {

        public static bool IsVisible(this TabPage tabPage)
        {
            if (tabPage.Parent == null)
                return false;
            else if (tabPage.Parent.Contains(tabPage))
                return true;
            else
                return false;
        }

        public static void HidePage(this TabPage tabPage)
        {
            TabControl parent = (TabControl)tabPage.Parent;
            parent.TabPages.Remove(tabPage);
        }

        public static void ShowPageInTabControl(this TabPage tabPage,TabControl parent)
        {
            parent.TabPages.Add(tabPage);
        }
    }
}

2- Add reference to ExtensionMethods namespace in your form code:

using ExtensionMethods;

3- Now you can use yourTabPage.IsVisible(); to check its visibility, yourTabPage.HidePage(); to hide it, and yourTabPage.ShowPageInTabControl(parentTabControl); to show it.

查看更多
Evening l夕情丶
4楼-- · 2019-01-03 06:29

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;
查看更多
放荡不羁爱自由
5楼-- · 2019-01-03 06:36

Visiblity property has not been implemented on the Tabpages, and there is no Insert method also.

You need to manually insert and remove tab pages.

Here is a work around for the same.

http://www.dotnetspider.com/resources/18344-Hiding-Showing-Tabpages-Tabcontrol.aspx

查看更多
We Are One
6楼-- · 2019-01-03 06:36

Solutions provided so far are way too complicated. Read the easiest solution at: http://www.codeproject.com/Questions/614157/How-to-Hide-TabControl-Headers

You could use this method to make them invisible at run time:

private void HideAllTabsOnTabControl(TabControl theTabControl)
{
  theTabControl.Appearance = TabAppearance.FlatButtons;
  theTabControl.ItemSize = new Size(0, 1);
  theTabControl.SizeMode = TabSizeMode.Fixed;
}
查看更多
Deceive 欺骗
7楼-- · 2019-01-03 06:36
    public static Action<Func<TabPage, bool>> GetTabHider(this TabControl container) {
        if (container == null) throw new ArgumentNullException("container");

        var orderedCache = new List<TabPage>();
        var orderedEnumerator = container.TabPages.GetEnumerator();
        while (orderedEnumerator.MoveNext()) {
            var current = orderedEnumerator.Current as TabPage;
            if (current != null) {
                orderedCache.Add(current);
            }
        }

        return (Func<TabPage, bool> where) => {
            if (where == null) throw new ArgumentNullException("where");

            container.TabPages.Clear();
            foreach (TabPage page in orderedCache) {
                if (where(page)) {
                    container.TabPages.Add(page);
                }
            }
        };
    }

Use it like this:

    var hider = this.TabContainer1.GetTabHider();
    hider((tab) => tab.Text != "tabPage1");
    hider((tab) => tab.Text != "tabpage2");

The original ordering of the tabs is kept in a List that is completely hidden inside the anonymous function. Keep a reference to the function instance and you retain your original tab order.

查看更多
登录 后发表回答