Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForm TabControl: How to hide/show tab headers dynamically?

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?

like image 825
newman Avatar asked Jan 22 '26 07:01

newman


2 Answers

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);
    }
}
like image 168
Hans Passant Avatar answered Jan 25 '26 07:01

Hans Passant


Grave digging a bit but I know of another solution. I have no idea where it came from but here it is:

In form load: (VB.NET)

Tabcontrol1.Region = New Region (New RectangleF(TabPage1.Left, TabPage1.Top, TabPage1.Width, TabPage1.Height))

Where TabControl1 is the name of your tab control and TabPage1 is the name of the first tab page in that control.

If you wanted to make it usable as a routine then you could do something like this:

Public Sub hideTabs(ByVal TC as TabControl)
    TC.Region = New Region(New RectangleF(TC.TabPages(0).Left,TC.TabPages(0).Top, TC.TabPages(0).Width, TC.TabPages(0).Height))
End Sub

It is that easy. What is nice about this is that the headers are not shown at runtime but they are visible at design time.

like image 30
Pow-Ian Avatar answered Jan 25 '26 08:01

Pow-Ian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!