How do I create a TabControl with no tab headers?

12,875

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
12,875
unicorn
Author by

unicorn

Updated on June 04, 2022

Comments

  • unicorn
    unicorn almost 2 years

    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. It's good for menus where various menu options change the screen contents.

  • unicorn
    unicorn about 13 years
    how exacly that help? say i need 3 screens.. how do i do it?
  • unicorn
    unicorn about 13 years
    @Nate Shoffner @Cody Gray I tried that, but when i dragged the new tab manager to the designer i get error: Falied to create component
  • unicorn
    unicorn about 13 years
    but how can i switch between them when i'n in designer? they on top eachother, no?
  • Cody Gray
    Cody Gray about 13 years
    @unicorn: Right-click on a panel control, and select "Send To Back" or "Bring To Front".
  • Cody Gray
    Cody Gray about 13 years
    @unicorn: What's the rest of the error say? Did you rebuild the project first? I know this works, so there has to be something else wrong.
  • unicorn
    unicorn about 13 years
    @Cody that will work, but the more screens i've got the messier this will be, i still think that tab control with invisible tab header is better..
  • Cody Gray
    Cody Gray about 13 years
    @unicorn: Another option is to design each of the "panels" as a separate UserControl, and then just add them to your form at run-time using code. That's generally the way I do this. This approach has the benefit of increasing encapsulation. All of your code is separated by the specific panel that it applies to, rather than all being dumped into one giant form class.
  • unicorn
    unicorn about 13 years
    the line 'base.WndProc(ref m);' was commet, fixed it and works fine, thanks :)
  • David Heffernan
    David Heffernan about 13 years
    +1 @Cody It's easy to get tied to the designer but for more complex UI like this then runtime coding usually makes it all simple again
  • user1703401
    user1703401 about 13 years
    I'm sure this has been answered before as well. Attribution is a big deal around here.
  • Cody Gray
    Cody Gray about 13 years
    @Hans: I don't remember where I originally learned this, so I didn't think provide any particular attribution. A Google search turns up this link, which appears the most likely candidate. I'll update the answer with attribution.
  • user1703401
    user1703401 over 12 years
  • miroxlav
    miroxlav over 8 years
    @CodyGray – perhaps you can add a note that the solution is not functionally complete, if one wants to use tab control as multi-page switcher. The loophole is that users are still able to change tabs using Ctrl+(Shift+)Tab or Ctrl+PgUp/PgDn even if they are hidden. Implementors should refer to post from June 22, 2012 at original source for effective way to avoid this.