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条回答
做自己的国王
2楼-- · 2019-01-03 06:20

No, this doesn't exist. You have to remove the tab and re-add it when you want it. Or use a different (3rd-party) tab control.

查看更多
干净又极端
3楼-- · 2019-01-03 06:20

Not sure about "Winforms 2.0" but this is tried and proven:

http://www.mostthingsweb.com/2011/01/hiding-tab-headers-on-a-tabcontrol-in-c/

查看更多
贼婆χ
4楼-- · 2019-01-03 06:21

Code Snippet for Hiding a TabPage

private void HideTab1_Click(object sender, EventArgs e)
{
    tabControl1.TabPages.Remove(tabPage1);
}

Code Snippet for Showing a TabPage

private void ShowTab1_Click(object sender, EventArgs e)
{
    tabControl1.TabPages.Add(tabPage1);
}
查看更多
Bombasti
5楼-- · 2019-01-03 06:23

I combined the answer from @Jack Griffin and the one from @amazedsaint (the dotnetspider code snippet respectively) into a single TabControlHelper.

The TabControlHelper lets you:

  • Show / Hide all tab pages
  • Show / Hide single tab pages
  • Keep the original position of the tab pages
  • Swap tab pages

public class TabControlHelper
{
    private TabControl _tabControl;
    private List<KeyValuePair<TabPage, int>> _pagesIndexed;
    public TabControlHelper(TabControl tabControl)
    {
        _tabControl = tabControl;
        _pagesIndexed = new List<KeyValuePair<TabPage, int>>();

        for (int i = 0; i < tabControl.TabPages.Count; i++)
        {
            _pagesIndexed.Add(new KeyValuePair<TabPage, int> (tabControl.TabPages[i], i ));
        }
    }

    public void HideAllPages()
    {
        for (int i = 0; i < _pagesIndexed.Count; i++)
        {
            _tabControl.TabPages.Remove(_pagesIndexed[i].Key);
        }
    }

    public void ShowAllPages()
    {
        for (int i = 0; i < _pagesIndexed.Count; i++)
        {
            _tabControl.TabPages.Add(_pagesIndexed[i].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.Where(p => p.Key == tabpage).First();
    }
}
查看更多
叼着烟拽天下
6楼-- · 2019-01-03 06:23
TabPage pageListe, pageDetay;
bool isDetay = false;

private void btnListeDetay_Click(object sender, EventArgs e)
{
    if (isDetay)
    {
        isDetay = false;
        tc.TabPages.Remove(tpKayit);
        tc.TabPages.Insert(0,pageListe);
    }
    else
    {
        tc.TabPages.Remove(tpListe);
        tc.TabPages.Insert(0,pageDetay);
        isDetay = true;
    }
}
查看更多
混吃等死
7楼-- · 2019-01-03 06:24
private System.Windows.Forms.TabControl _tabControl;
private System.Windows.Forms.TabPage _tabPage1;
private System.Windows.Forms.TabPage _tabPage2;

...
// Initialise the controls
...

// "hides" tab page 2
_tabControl.TabPages.Remove(_tabPage2);

// "shows" tab page 2
// if the tab control does not contain tabpage2
if (! _tabControl.TabPages.Contains(_tabPage2))
{
    _tabControl.TabPages.Add(_tabPage2);
}
查看更多
登录 后发表回答