C# 如何从 TabControl 隐藏 TabPage
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/552579/
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
How to hide TabPage from TabControl
提问by Tomasz Smykowski
How to hide TabPage from TabControl in WinForms 2.0?
如何在 WinForms 2.0 中从 TabControl 中隐藏 TabPage?
采纳答案by Marc Gravell
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 方)选项卡控件。
回答by amazedsaint
Visiblity property has not been implemented on the Tabpages, and there is no Insert method also.
Tabpages 没有实现 Visiblity 属性,也没有 Insert 方法。
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
http://www.dotnetspider.com/resources/18344-Hiding-Showing-Tabpages-Tabcontrol.aspx
回答by moonshine
Code Snippet for Hiding a TabPage
隐藏标签页的代码片段
private void HideTab1_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Remove(tabPage1);
}
Code Snippet for Showing a TabPage
用于显示 TabPage 的代码片段
private void ShowTab1_Click(object sender, EventArgs e)
{
tabControl1.TabPages.Add(tabPage1);
}
回答by profimedica
Variant 1
变体 1
In order to avoid visual klikering you might need to use:
为了避免视觉klikering,您可能需要使用:
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
变体 2
myTabPage.parent = null;
myTabPage.parent = myTabControl;
回答by Kevin Godwin
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.
然后我们可以使用标签的可见属性作为替代。如果有人确实走这条路,请不要忘记处理键盘敲击或可见性事件。您不希望左右光标键暴露您试图隐藏的选项卡。
回答by Rob
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.
选项卡的原始顺序保存在一个完全隐藏在匿名函数内部的列表中。保留对函数实例的引用,并保留原始的 Tab 键顺序。
回答by Hyman Griffin
+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:
+1 为微软:-)。
我设法这样做:(
假设您有一个Next
显示下一个 TabPage的按钮 -tabSteps
是 Tab 控件的名称)
在启动时,将所有标签页保存在适当的列表中。
当用户按下Next
按钮时,删除选项卡控件中的所有 TabPages,然后使用适当的索引添加它:
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);
}
}
回答by Abuleen
you can set the parent of the tabpage to null for hiding and to show just set tabpage parent to the tabcontrol
您可以将 tabpage 的父级设置为 null 以进行隐藏并仅显示将 tabpage 父级设置为 tabcontrol
回答by sree ranjith c.k
// inVisible
TabPage page2 = tabControl1.TabPages[0];
page2.Visible= false;
//Visible
page2.Visible= true;
// disable
TabPage page2 = tabControl1.TabPages[0];
page2.Enabled = false;
// enable
page2.Enabled = true;
//Hide
tabCtrlTagInfo.TabPages(0).Hide()
tabCtrlTagInfo.TabPages(0).Show()
Just copy paste and try it,the above code has been tested in vs2010, it works.
直接复制粘贴试试,上面的代码已经在vs2010测试过了,可以用。
回答by user720694
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.
好吧,如果您不想弄乱现有代码而只想隐藏选项卡,您可以修改编译器生成的代码以注释将选项卡添加到 tabcontrol 的行。
For example: The following line adds a tab named "readformatcardpage" to a Tabcontrol named "tabcontrol"
例如:以下行将名为“readformatcardpage”的选项卡添加到名为“tabcontrol”的 Tabcontrol
this.tabcontrol.Controls.Add(this.readformatcardpage);
this.tabcontrol.Controls.Add(this.readformatcardpage);
The following will prevent addition of the tab to the tabcontrol
以下将阻止将选项卡添加到 tabcontrol
//this.tabcontrol.Controls.Add(this.readformatcardpage);
//this.tabcontrol.Controls.Add(this.readformatcardpage);