Winforms – WinForm TabControl: How to hide/show tab headers dynamically

tabcontrolwinforms

I want to make my tabControl a little smarter to save some screen real estate: Don't show tab header if there is only one tab and show tab headers if there are two or more tabs.

I know that you can hide the tab header completely as suggested at How do I create a TabControl with no tab headers?. The problem with this approach is that, once hidden, I cannot show the tab header again. Or did I miss something?

Best Answer

Crediting the guy that actually came up with the idea:

using System;
using System.ComponentModel;
using System.Windows.Forms;

public class WizardPages : TabControl {
    private bool tabsVisible;

    [DefaultValue(false)]
    public bool TabsVisible {
        get { return tabsVisible; }
        set {
            if (tabsVisible == value) return;
            tabsVisible = value;
            RecreateHandle();
        }
    }

    protected override void WndProc(ref Message m) {
        // Hide tabs by trapping the TCM_ADJUSTRECT message
        if (m.Msg == 0x1328) {
            if (!tabsVisible && !DesignMode) {
                m.Result = (IntPtr)1;
                return;
            }
        }
        base.WndProc(ref m);
    }
}
Related Topic