简体   繁体   English

如何创建没有标签标题的TabControl?

[英]How do I create a TabControl with no tab headers?

How do I make a tab manager that doesn't show the tab headers? 如何创建不显示选项卡标题的选项卡管理器?

This is a winforms application, and the purpose of using a tab manager is so the display content can only be changed through code. 这是一个winforms应用程序,使用选项卡管理器的目的是只能通过代码更改显示内容。 It's good for menus where various menu options change the screen contents. 它适用于各种菜单选项更改屏幕内容的菜单。

Hiding the tabs on a standard TabControl is pretty simple, once you know the trick. 一旦你知道了这个技巧,隐藏标准TabControl上的标签非常简单。 The tab control is sent a TCM_ADJUSTRECT message when it needs to adjust the tab size, so we just need to trap that message. 当需要调整选项卡大小时,选项卡控件会发送TCM_ADJUSTRECT消息 ,因此我们只需要捕获该消息。 (I'm sure this has been answered before, but posting the code is easier than searching for it.) (我确信之前已经回答过,但发布代码比搜索代码更容易。)

Add the following code to a new class in your project, recompile, and use the CustomTabControl class instead of the built-in control: 将以下代码添加到项目中的新类,重新编译,并使用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);
    }
}

(Code sample originally taken from Dot Net Thoughts .) (代码示例最初来自Dot Net Ideas 。)

Note that this will not work properly for tab headers positioned on the sides or the bottom. 请注意,对于位于侧面或底部的标签页眉,这将无法正常工作。 But not only does that just look weird, you won't be able to see the tabs at run-time anyway. 但这不仅看起来很奇怪,而且无论如何都无法在运行时看到标签。 Just put them on the top where they belong. 把它们放在它们所属的顶部。

是的,如果它是Web应用程序,您可以使用相同的位置构建自己的DIV,并根据您的需要隐藏/显示。

Along with everybody else, I find your question a bit confusing. 与其他人一起,我发现你的问题有点令人困惑。 I've used this method found here before. 我用发现这个方法在此之前。 Using this way you have a single property you can change as to whether you want to show the tab headers or not. 使用这种方式,您可以更改一个属性,以确定是否要显示选项卡标题。

I guess, that using panels is the simplest solution. 我想,使用面板是最简单的解决方案。 In addition, I suggest using my (free, opensource) VisualStateManager to simplify switching and eliminate lots of .Enabled = true horrors. 另外,我建议使用我的(free,opensource) VisualStateManager来简化切换并消除大量的.Enabled = true恐怖。

Package is available on Nuget . Nuget提供套餐

Just write this code: 只需编写此代码:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM