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条回答
smile是对你的礼貌
2楼-- · 2019-01-03 06:37

+1 for microsoft :-) .
I managed to do it this way:
(it assumes you have a Next button that displays the next TabPage - tabSteps is the name of the Tab control)
At start up, save all the tabpages in a proper list.
When user presses Next button, remove all the TabPages in the tab control, then add that with the proper index:

int step = -1;
List<TabPage> savedTabPages;

private void FMain_Load(object sender, EventArgs e) {
    // save all tabpages in the list
    savedTabPages = new List<TabPage>();
    foreach (TabPage tp in tabSteps.TabPages) {
        savedTabPages.Add(tp);
    }
    SelectNextStep();
}

private void SelectNextStep() {
    step++;
    // remove all tabs
    for (int i = tabSteps.TabPages.Count - 1; i >= 0 ; i--) {
            tabSteps.TabPages.Remove(tabSteps.TabPages[i]);
    }

    // add required tab
    tabSteps.TabPages.Add(savedTabPages[step]);
}

private void btnNext_Click(object sender, EventArgs e) {
    SelectNextStep();
}

Update

public class TabControlHelper {
    private TabControl tc;
    private List<TabPage> pages;
    public TabControlHelper(TabControl tabControl) {
        tc = tabControl;
        pages = new List<TabPage>();
        foreach (TabPage p in tc.TabPages) {
            pages.Add(p);
        }
    }

    public void HideAllPages() {
        foreach(TabPage p in pages) {
            tc.TabPages.Remove(p);
        }
    }

    public void ShowAllPages() {
        foreach (TabPage p in pages) {
            tc.TabPages.Add(p);
        }
    }

    public void HidePage(TabPage tp) {
        tc.TabPages.Remove(tp);
    }        

    public void ShowPage(TabPage tp) {
        tc.TabPages.Add(tp);
    }
}  
查看更多
冷血范
3楼-- · 2019-01-03 06:40

As a cheap work around, I've used a label to cover up the tabs I wanted to hide.

We can then use the visible prop of the label as a substitute. If anyone does go this route, don't forget to handle keyboard strokes or visibility events. You wouldn't want the left right cursor keys exposing the tab you're trying to hide.

查看更多
Fickle 薄情
4楼-- · 2019-01-03 06:42

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;
    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) _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;
    }
}
查看更多
老娘就宠你
5楼-- · 2019-01-03 06:42

I've used the same approach but the problem is that when tab page was removed from the tab control TabPages list, it is removed from the tab page Controls list also. And it is not disposed when form is disposed.

So if you have a lot of such "hidden" tab pages, you can get windows handle quota exceeded error and only application restart will fix it.

查看更多
贼婆χ
6楼-- · 2019-01-03 06:43

Variant 1

In order to avoid visual klikering you might need to use:

bindingSource.RaiseListChangeEvent = false 

or

myTabControl.RaiseSelectedIndexChanged = false

Remove a tab page:

myTabControl.Remove(myTabPage);

Add a tab page:

myTabControl.Add(myTabPage);

Insert a tab page at specific location:

myTabControl.Insert(2, myTabPage);

Do not forget to revers the changes:

bindingSource.RaiseListChangeEvent = true;

or

myTabControl.RaiseSelectedIndexChanged = true;

Variant 2

myTabPage.parent = null;
myTabPage.parent = myTabControl;
查看更多
冷血范
7楼-- · 2019-01-03 06:45

you can set the parent of the tabpage to null for hiding and to show just set tabpage parent to the tabcontrol

查看更多
登录 后发表回答