C# 禁用和隐藏 TabPage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12895092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:52:12  来源:igfitidea点击:

Disable and hide a TabPage

c#.netwinformstabcontroltabpage

提问by Aan

How I can make a TabPagein a TabControlvisible/hidden and enabled/disabled?

我如何才能TabPageTabControl可见/隐藏和启用/禁用?

采纳答案by BugFinder

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.

也不要从顶部的列表中删除选项卡。

回答by Gianni B.

What about tabPage.Enabledand tabPage.Visibleproperties?

怎么样tabPage.EnabledtabPage.Visible性质?

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

仅供参考:http: //msdn.microsoft.com/en-us/library/8fb09fh2.aspx

回答by Nikola Davidovic

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

我不知道启用/禁用(也许尝试禁用它的所有控件)。如果您想隐藏它们,只需将它们从 Items 集合中删除。如果您希望它们再次可见,您可以再次将它们添加回控件。不过,您必须注意它们的顺序(将它们的引用存储在某个列表中,或者您可以有两个列表,其中包含对可见和不可见的 TabPage 的引用)。

回答by Otiel

  • Enable / disable

    The tabPage.Enabledseems 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 thisfor instance.

  • Show / hide

    There is an existing tabPage.Visibleproperty 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);
    
  • 启用/禁用

    tabPage.Enabled似乎是工作的罚款,但被标记为“未使用”:

    此 API 支持 .NET Framework 基础结构,不应直接从您的代码中使用。
    此成员对于此控件没有意义。

    因此,您应该通过禁用选项卡中的每个控件来禁用选项卡页。例如看这个

  • 显示隐藏

    有一个现有的 tabPage.Visible属性,但它似乎没有任何影响。此外,它还被标记为“不可使用”,msdn 建议将标签页从标签控件中移除以隐藏它:

    // Hide the tab page
    tabControl.TabPages.Remove(tabPage1);
    // Show the tab page (insert it to the correct position)
    tabControl.TabPages.Insert(0, tabPage1);
    

回答by John S.

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

我也有这个疑问。tabPage.Visible 没有像前面所说的那样实现,这是一个很大的帮助(+1)。我发现您可以覆盖控件,这将起作用。有点尸检,但我想在这里发布我的解决方案给其他人......

    [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;
    }
}

回答by sindhu jampani

we can Enable or disable tab pages by using TABPAGE.ENABLE=trueand TABPAGE.ENABLE=FALSE.

我们可以使用TABPAGE.ENABLE=true和启用或禁用标签页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;
     }

回答by ISB

// 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;

回答by THE INN-VISIBLE

Put tabpageinto paneland hide panelusing

tabpage进入panel和隐藏panel使用

this.panel1.visible=false;

It is working for me !

它对我有用!

回答by user3304385

//Move&Add is not good answer   
this.tabPage1.Parent = null; // hide    
this.tabPage1.Parent = this.tabControl1; //show

回答by kokbira

Based on @Otiel's answer, I did those two functions:

根据@Otiel 的回答,我完成了这两个功能:

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
}