feather
feather

Reputation: 85

c# : how to remove tabcontrol border?

In my form, I'm using a tabcontrol. I want to hide the tab headers and border both. I can do either one, if I try to hide the headers then the border becomes visible. Can anyone help me, please? thanks and here's my code:

public Form3()
{
    InitializeComponent();
    this.NativeTabControl1 = new NativeTabControl();

    this.NativeTabControl1.AssignHandle(this.tabControl1.Handle);

}

private NativeTabControl NativeTabControl1;

private class NativeTabControl : NativeWindow
{
    protected override void WndProc(ref Message m)
    {
        if ((m.Msg == TCM_ADJUSTRECT))
        {
            RECT rc = (RECT)m.GetLParam(typeof(RECT));
            //Adjust these values to suit, dependant upon Appearance
            rc.Left -= 3;
            rc.Right += 3;
            rc.Top -= 3;
            rc.Bottom += 3;
            Marshal.StructureToPtr(rc, m.LParam, true);
        }
        base.WndProc(ref m);
    }

    private const Int32 TCM_FIRST = 0x1300;
    private const Int32 TCM_ADJUSTRECT = (TCM_FIRST + 40);
    private struct RECT
    {
        public Int32 Left;
        public Int32 Top;
        public Int32 Right;
        public Int32 Bottom;
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        //hides tabcontrol headers
        tabControl1.Appearance = TabAppearance.Buttons;
        tabControl1.ItemSize = new Size(0, 1);
        tabControl1.SizeMode = TabSizeMode.Fixed;
    }
}

Upvotes: 4

Views: 21584

Answers (2)

Koopakiller
Koopakiller

Reputation: 2884

There is another thread on Stackoverflow, which provides some ideas to hide the tab row of the TabControl in Windows Forms.
My favorite one is to override WndProc and set the Multiline property to true.

public partial class TabControlWithoutHeader : TabControl
{
    public TabControlWithoutHeader()
    {
        if (!this.DesignMode) this.Multiline = true;
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x1328 && !this.DesignMode)
            m.Result = new IntPtr(1);
        else
            base.WndProc(ref m);
    }
}

I tested the code on Windows 8.1 and see neither the tabs nor a border line. So I think you do not need to use code like you posted.

Upvotes: 4

Jarod Kientz
Jarod Kientz

Reputation: 131

I would usually suggest doing this in the xaml not C# (I'm a WPF developer) I believe you can do it in the C# as well by naming both the TabControl as well as each Tab itself.

tabControlName.BorderBrush = null;
///^Gets rid of the TabControl's border.

tabName1.Height = 0;
tabName2.Height = 0;
tabNameETC.Height = 0;
///^Removes the tabs(headers) if you have the TabControl.TabStripPlacement set to left 
/// or right, then use the following instead:

tabName1.Width = 0
tabName2.Width = 0
tabNameETC.Width = 0

Upvotes: 0

Related Questions