Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 922
unicorn Avatar asked Dec 02 '22 03:12

unicorn


1 Answers

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.

like image 128
Cody Gray Avatar answered Dec 03 '22 17:12

Cody Gray