[英]How do I create a TabControl with no tab headers?
如何创建不显示选项卡标题的选项卡管理器?
这是一个winforms应用程序,使用选项卡管理器的目的是只能通过代码更改显示内容。 它适用于各种菜单选项更改屏幕内容的菜单。
一旦你知道了这个技巧,隐藏标准TabControl
上的标签非常简单。 当需要调整选项卡大小时,选项卡控件会发送TCM_ADJUSTRECT
消息 ,因此我们只需要捕获该消息。 (我确信之前已经回答过,但发布代码比搜索代码更容易。)
将以下代码添加到项目中的新类,重新编译,并使用CustomTabControl
类而不是内置控件:
class CustomTabControl : TabControl
{
private const int TCM_ADJUSTRECT = 0x1328;
protected override void WndProc(ref Message m)
{
// Hide the tab headers at run-time
if (m.Msg == TCM_ADJUSTRECT && !DesignMode)
{
m.Result = (IntPtr)1;
return;
}
// call the base class implementation
base.WndProc(ref m);
}
}
(代码示例最初来自Dot Net Ideas 。)
请注意,对于位于侧面或底部的标签页眉,这将无法正常工作。 但这不仅看起来很奇怪,而且无论如何都无法在运行时看到标签。 把它们放在它们所属的顶部。
是的,如果它是Web应用程序,您可以使用相同的位置构建自己的DIV,并根据您的需要隐藏/显示。
与其他人一起,我发现你的问题有点令人困惑。 我用发现这个方法在此之前。 使用这种方式,您可以更改一个属性,以确定是否要显示选项卡标题。
我想,使用面板是最简单的解决方案。 另外,我建议使用我的(free,opensource) VisualStateManager
来简化切换并消除大量的.Enabled = true
恐怖。
只需编写此代码:
// Contains and propagates information about current page
private SwitchCondition<int> settingPageCondition;
// Controls state of specific controls basing on given SwitchCondition
private VisualStateSwitchController<int> settingPageController;
// (...)
private void InitializeActions()
{
// Initialize with possible options
settingPageCondition = new SwitchCondition<int>(0, 1);
settingPageController = new VisualStateSwitchController<int>(
null, // Enabled is not controlled
null, // Checked is not controlled
settingPageCondition, // Visible is controller by settingPageCondition
new SwitchControlSet<int>(0, pGeneral), // State 0 controls pGeneral
new SwitchControlSet<int>(1, pParsing)); // State 1 controls pParsing
}
// (...)
public void MainForm()
{
InitializeComponent();
InitializeActions();
}
// (...)
// Wat to set specific page
settingPageCondition.Current = 0;
在编辑和评论使问题更清楚之后,我认为处理这个问题的正常方法是使用多个面板而不是标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.