如何动态显示两个Winforms选项卡之一?

时间:2022-12-30 15:50:32

I have a TabControl with 5 tabs, and the contents of one of the tabs depends on some conditions or modes - Sometimes it needs to show one set of controls, on other times it should show an entirely different set of controls.

我有一个带有5个选项卡的TabControl,其中一个选项卡的内容取决于某些条件或模式 - 有时它需要显示一组控件,有时它应该显示一组完全不同的控件。

What is the easiest way to achieve this? I tried setting up two different tabs, and using something like tab.Enabled / Visible - but couldn't find such attributes on the tabs.

实现这一目标的最简单方法是什么?我尝试设置两个不同的选项卡,并使用tab.Enabled / Visible之类的东西 - 但在选项卡上找不到这样的属性。

I would like the ability the switch modes of operation - go from displaying one tab, to displaying the other tab, and back again. Mind you, I don't want to change which tab is active, I want to completely hide one tab, then show it and hide another tab.

我希望切换操作模式的能力 - 从显示一个选项卡,到显示另一个选项卡,再返回。请注意,我不想更改哪个选项卡处于活动状态,我想完全隐藏一个选项卡,然后显示它并隐藏另一个选项卡。

4 个解决方案

#1


Here is a simple fix (hack)

这是一个简单的修复(hack)

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

#2


The linked solution is unnecessarily complex for what you want to do. Here's a snippet the does what you want. It's based on a form with 3 or more tabs. Suppose at any given time you want either tab 2 or tab 3 to display, but never both. If the form has more tabs, they display after tab 2 or 3, so your alternating tab will always display at the same index position. Also suppose on tab 1 there are a pair of radio buttons named 'tab2button' and 'tab3button' that toggle which of the tabs is displayed. The checkchanged event for the radio buttons would contain this (c++):

对于您想要执行的操作,链接的解决方案不必要地复杂。这是一个你想要的片段。它基于具有3个或更多选项卡的表单。假设在任何给定时间您需要显示选项卡2或选项卡3,但不要同时显示两者。如果表单有更多选项卡,它们会显示在选项卡2或3之后,因此您的交替选项卡将始终显示在相同的索引位置。另外,在选项卡1上还有一对名为“tab2button”和“tab3button”的单选按钮,用于切换显示哪个选项卡。单选按钮的checkchanged事件将包含此(c ++):

             if (this->tab2button->Checked)
             {
                 if(tabControl1->TabPages->Contains(tabPage3))
                 {
                     tabControl1->TabPages->Remove(tabPage3);
                 }
                 if(!tabControl1->TabPages->Contains(tabPage2))
                 {
                     tabControl1->TabPages->Insert(1,tabPage2);
                 }

             }
             else if (this->tab3button->Checked)
             {
                 if(tabControl1->TabPages->Contains(tabPage2))
                 {
                     tabControl1->TabPages->Remove(tabPage2);
                 }
                 if(!tabControl1->TabPages->Contains(tabPage3))
                 {
                     tabControl1->TabPages->Insert(1,tabPage3);
                 }
             }

#3


I don't know of an elegant solution. The easiest for a small application seems to be to TabPages.Add/Insert/Remove as suggested above.

我不知道一个优雅的解决方案。对于小应用程序来说最简单的似乎是TabPages.Add / Insert / Remove,如上所述。

In our large application, we override the TabControl and the TabPage class-- call them MyTabControl and MyTabPage-- and added our own Visible property. Setting tabPage.Visible = false causes the tab page to remove itself from the tabControl.TabPages collection. Making the page visible again causes it to insert itself to the tab control's collection at the original index. The Visible property made the rest of our code a little easier to manage.

在我们的大型应用程序中,我们覆盖TabControl和TabPage类 - 称之为MyTabControl和MyTabPage--并添加了我们自己的Visible属性。设置tabPage.Visible = false会导致标签页从tabControl.TabPages集合中删除。使页面再次可见会导致它将自身插入到原始索引处的选项卡控件的集合中。 Visible属性使我们的其余代码更容易管理。

However, the subclasses made our MyTabControl subclass slightly harder to design. TabControl.TabPages is a TabPageCollection. The Visual Studio designer wants to create and add TabPages to it. We needed it to hold our MyTabPage subclasses, so we also created a MyTabPageCollection.

但是,子类使我们的MyTabControl子类稍微难以设计。 TabControl.TabPages是一个TabPageCollection。 Visual Studio设计器想要为其创建和添加TabPages。我们需要它来保存我们的MyTabPage子类,所以我们还创建了一个MyTabPageCollection。

We also have many places where different groups of controls on a tab are visible depending on user choices. We group controls on different panels and show the panel that corresponds to the appropriate settings settings.

我们还有许多地方,根据用户的选择,选项卡上的不同控件组是可见的。我们将控件分组到不同的面板上,并显示与相应设置设置对应的面板。

#4


I can't believe I haven't seen a proper solution for something so easy. I know in the past I've shown and hidden the tabs.

我简直不敢相信我没有看到适当的解决方案。我知道在过去我已经显示并隐藏了标签。

tabControl.SelectTab(index);

Bingo! No remove, no insert and no shuffling.

答对了!没有删除,没有插入和没有改组。

-Holt

#1


Here is a simple fix (hack)

这是一个简单的修复(hack)

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

#2


The linked solution is unnecessarily complex for what you want to do. Here's a snippet the does what you want. It's based on a form with 3 or more tabs. Suppose at any given time you want either tab 2 or tab 3 to display, but never both. If the form has more tabs, they display after tab 2 or 3, so your alternating tab will always display at the same index position. Also suppose on tab 1 there are a pair of radio buttons named 'tab2button' and 'tab3button' that toggle which of the tabs is displayed. The checkchanged event for the radio buttons would contain this (c++):

对于您想要执行的操作,链接的解决方案不必要地复杂。这是一个你想要的片段。它基于具有3个或更多选项卡的表单。假设在任何给定时间您需要显示选项卡2或选项卡3,但不要同时显示两者。如果表单有更多选项卡,它们会显示在选项卡2或3之后,因此您的交替选项卡将始终显示在相同的索引位置。另外,在选项卡1上还有一对名为“tab2button”和“tab3button”的单选按钮,用于切换显示哪个选项卡。单选按钮的checkchanged事件将包含此(c ++):

             if (this->tab2button->Checked)
             {
                 if(tabControl1->TabPages->Contains(tabPage3))
                 {
                     tabControl1->TabPages->Remove(tabPage3);
                 }
                 if(!tabControl1->TabPages->Contains(tabPage2))
                 {
                     tabControl1->TabPages->Insert(1,tabPage2);
                 }

             }
             else if (this->tab3button->Checked)
             {
                 if(tabControl1->TabPages->Contains(tabPage2))
                 {
                     tabControl1->TabPages->Remove(tabPage2);
                 }
                 if(!tabControl1->TabPages->Contains(tabPage3))
                 {
                     tabControl1->TabPages->Insert(1,tabPage3);
                 }
             }

#3


I don't know of an elegant solution. The easiest for a small application seems to be to TabPages.Add/Insert/Remove as suggested above.

我不知道一个优雅的解决方案。对于小应用程序来说最简单的似乎是TabPages.Add / Insert / Remove,如上所述。

In our large application, we override the TabControl and the TabPage class-- call them MyTabControl and MyTabPage-- and added our own Visible property. Setting tabPage.Visible = false causes the tab page to remove itself from the tabControl.TabPages collection. Making the page visible again causes it to insert itself to the tab control's collection at the original index. The Visible property made the rest of our code a little easier to manage.

在我们的大型应用程序中,我们覆盖TabControl和TabPage类 - 称之为MyTabControl和MyTabPage--并添加了我们自己的Visible属性。设置tabPage.Visible = false会导致标签页从tabControl.TabPages集合中删除。使页面再次可见会导致它将自身插入到原始索引处的选项卡控件的集合中。 Visible属性使我们的其余代码更容易管理。

However, the subclasses made our MyTabControl subclass slightly harder to design. TabControl.TabPages is a TabPageCollection. The Visual Studio designer wants to create and add TabPages to it. We needed it to hold our MyTabPage subclasses, so we also created a MyTabPageCollection.

但是,子类使我们的MyTabControl子类稍微难以设计。 TabControl.TabPages是一个TabPageCollection。 Visual Studio设计器想要为其创建和添加TabPages。我们需要它来保存我们的MyTabPage子类,所以我们还创建了一个MyTabPageCollection。

We also have many places where different groups of controls on a tab are visible depending on user choices. We group controls on different panels and show the panel that corresponds to the appropriate settings settings.

我们还有许多地方,根据用户的选择,选项卡上的不同控件组是可见的。我们将控件分组到不同的面板上,并显示与相应设置设置对应的面板。

#4


I can't believe I haven't seen a proper solution for something so easy. I know in the past I've shown and hidden the tabs.

我简直不敢相信我没有看到适当的解决方案。我知道在过去我已经显示并隐藏了标签。

tabControl.SelectTab(index);

Bingo! No remove, no insert and no shuffling.

答对了!没有删除,没有插入和没有改组。

-Holt