C# 如何禁用 TabControl 中的选项卡?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/418006/
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 can I disable a tab inside a TabControl?
提问by Gold
采纳答案by Hans Passant
The TabPageclass hides the Enabled property. That was intentional as there is an awkward UI design problem with it. The basic issue is that disabling the page does not also disable the tab. And if try to work around that by disabling the tab with the Selecting event then it does not work when the TabControl has only one page.
在TabPage的类隐藏Enabled属性。这是故意的,因为它存在一个尴尬的 UI 设计问题。基本问题是禁用页面不会同时禁用选项卡。如果尝试通过使用 Selecting 事件禁用选项卡来解决这个问题,那么当 TabControl 只有一页时它不起作用。
If these usability problems do not concern you then keep in mind that the property still works, it is merely hidden from IntelliSense. If the FUD is uncomfortable then you can simply do this:
如果这些可用性问题与您无关,那么请记住该属性仍然有效,它只是对 IntelliSense 隐藏。如果 FUD 不舒服,那么您可以简单地执行以下操作:
public static void EnableTab(TabPage page, bool enable) {
foreach (Control ctl in page.Controls) ctl.Enabled = enable;
}
回答by Cédric Guillemette
Cast your TabPage to a Control, then set the Enabled property to false.
将 TabPage 转换为 Control,然后将 Enabled 属性设置为 false。
((Control)this.tabPage).Enabled = false;
Therefore, the tabpage's header will still be enabled but its contents will be disabled.
因此,标签页的标题仍将启用,但其内容将被禁用。
回答by Martijn Laarman
The only way is to catch the Selecting
event and prevent a tab from being activated.
唯一的方法是捕获Selecting
事件并防止选项卡被激活。
回答by Aaron Smith
I've removed tab pages in the past to prevent the user from clicking them. This probably isn't the best solution though because they may need to see that the tab page exists.
我过去删除了标签页以防止用户点击它们。这可能不是最好的解决方案,因为他们可能需要查看标签页是否存在。
回答by Stormenet
You could register the "Selecting" event and cancel the navigation to the tab page:
您可以注册“选择”事件并取消导航到标签页:
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (e.TabPage == tabPage2)
e.Cancel = true;
}
Another idea is to put all the controls on the tabpage in a Panel control and disable the panel! Smiley
另一个想法是将标签页上的所有控件放在一个面板控件中并禁用面板!笑脸
You could also remove the tabpage from the tabControl1.TabPages collection. That would hide the tabpage.
您还可以从 tabControl1.TabPages 集合中删除标签页。这将隐藏标签页。
Credits go to littleguru @ Channel 9.
学分转到littleguru@第 9 频道。
回答by jcollum
I had to handle this a while back. I removed the Tab from the TabPages collection (I think that's it) and added it back in when the conditions changed. But that was only in Winforms where I could keep the tab around until I needed it again.
不久前我不得不处理这个问题。我从 TabPages 集合中删除了 Tab(我认为就是这样),并在条件发生变化时将其重新添加。但这只是在 Winforms 中我可以保留标签直到我再次需要它。
回答by Stewart
Presumably, you want to see the tab in the tab control, but you want it to be "disabled" (i.e., greyed, and unselectable). There is no built-in support for this, but you can override the drawing mechanism to give the desired effect.
据推测,您希望在选项卡控件中看到该选项卡,但您希望它被“禁用”(即,变灰且不可选择)。对此没有内置支持,但您可以覆盖绘图机制以获得所需的效果。
An example of how to do this is provided here.
此处提供了如何执行此操作的示例。
The magic is in this snippet from the presented source, and in the DisableTab_DrawItem method:
神奇之处在于来自提供的源代码的这个片段,以及 DisableTab_DrawItem 方法:
this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
this.tabControl1.DrawItem += new DrawItemEventHandler( DisableTab_DrawItem );
回答by jay_t55
MyTabControl.SelectedTab.Enabled = false;
回答by sahran
This will remove the tab page, but you'll need to re-add it when you need it:
这将删除标签页,但您需要在需要时重新添加它:
tabControl1.Controls.Remove(tabPage2);
If you are going to need it later, you might want to store it in a temporary tabpage before the remove and then re-add it when needed.
如果您以后需要它,您可能希望在删除之前将其存储在一个临时标签页中,然后在需要时重新添加它。
回答by KWB Tech
Using events, and the properties of the tab control you can enable/disable what you want when you want. I used one bool that is available to all methods in the mdi child form class where the tabControl is being used.
使用事件和选项卡控件的属性,您可以在需要时启用/禁用所需内容。我使用了一个可用于使用 tabControl 的 mdi 子窗体类中的所有方法的 bool。
Remember the selecting event fires every time any tab is clicked. For large numbers of tabs a "CASE" might be easier to use than a bunch of ifs.
请记住,每次单击任何选项卡时都会触发选择事件。对于大量选项卡,“CASE”可能比一堆 if 更容易使用。
public partial class Form2 : Form
{
bool formComplete = false;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
formComplete = true;
tabControl1.SelectTab(1);
}
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (tabControl1.SelectedTab == tabControl1.TabPages[1])
{
tabControl1.Enabled = false;
if (formComplete)
{
MessageBox.Show("You will be taken to next tab");
tabControl1.SelectTab(1);
}
else
{
MessageBox.Show("Try completing form first");
tabControl1.SelectTab(0);
}
tabControl1.Enabled = true;
}
}
}