开发者

How do I create a TabControl with no tab headers?

开发者 https://www.devze.com 2023-02-08 15:50 出处:网络
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 con开发者_JAVA技巧tent can only be changed thr

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 con开发者_JAVA技巧tent can only be changed through code. 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. 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. (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:

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

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.


Right, if it's web application, you can build your own DIV with the same placement and hide/show as per your needs.


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.


After the edit and comments made the question more clear, I think the normal way to handle this is to use multiple panels rather than tabs.


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.

Package is available on 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号