Hide Tab Header on C# TabControl

2019-01-04 00:53发布

I am developing a Windows Form Application with several pages. I am using a TabControl to implement this. Instead of using the header to switch between tabs, I want my application to control this e.g. the next tab should open after the user has filled in a text box and clicked the next button.

9条回答
迷人小祖宗
2楼-- · 2019-01-04 01:17

Create new UserControl, name it for example TabControlWithoutHeader and change inherited UserControl to TabControl and add some code. Result code should look like:

public partial class TabControlWithoutHeader: TabControl
{
    public TabControlWithoutHeader()
    {
        InitializeComponent();
    }

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

After compile you will have TabControlWithoutHeader control in ToolBox. Drop it on form, in designer you will see headers, but at runtime they'll be hidden. If you want to hide them in designer too, then remove && !DesignMode.

Hope that helps.

http://social.msdn.microsoft.com/Forums/windows/en-US/c290832f-3b84-4200-aa4a-7a5dc4b8b5bb/tabs-in-winform?forum=winforms

查看更多
趁早两清
3楼-- · 2019-01-04 01:19

To complement Hans Passant's existing answer, I've found four ways to hide the arrows from the user when the numbers of tabs exceeds the width of the TablessControl. No single solution is necessarily perfect for everyone, but may be for you (or at least a combination of them).

Solution 1:

Simply enable Multiline. This will prevent the arrows from appearing in the first place. However, bear in mind, you may lose WYSIWYG in the designer because the vertical space will be adjusted downwards vertically, and controls within the TablessControl may even be 'chopped off' at the bottom (again, only in developer mode though).

Solution 2:

A more advanced solution which solves the WYSIWYG problem above is to only enable Multiline once the program gets running. Simply add this constructor to the TablessControl class:

public TablessControl()
{
    bool designMode = (LicenseManager.UsageMode == LicenseUsageMode.Designtime);
    if (!designMode) Multiline = true;      
}

To the developer, they will still appear as a single line of tabs.

Solution 3:

Decrease the font size of the TablessControl. Each tab should shrink accordingly. Since the user never gets to see the tabs, it shouldn't matter much if you set the font sizes to even 4pt.

However be careful, because the TablessControl's contents may also be resized. If this happens, re-edit the font size for each widget inside, and at that point, they'll thankfully stay at that size even if you then decide to re-change the main TablessControl's font size again.

This approach also has the advantage of more closely showing the true WYSIWYG vertical real-estate to the developer (which can look fine for the user, but may be cut off slightly at the bottom in the designer due to the height of the tabs).

This solution can be combined with Solution 1 and 2 for accumulated advantages.

Solution 4:

This solution isn't necessarily so great if any of the tabs have text which are long. Thanks to Hans for suggesting it.

First set the TablessControl's SizeMode to 'Fixed', and then reduce the TablessControl's ItemSize Width property to a smaller number to reduce each tab's width. Feel free also to adjust the ItemSize Height property to help address the aforementioned WYSIWYG issue, though Solution 3 may be more helpful for that problem.

This solution can be combined with the above solutions to further accumulate advantages.

查看更多
混吃等死
4楼-- · 2019-01-04 01:21

This solution appears to work well - How to hide tabs in the tab control?

  1. Insert Tabcontrol into a form, the default name being tabcontrol1.

  2. Ensure that tabcontrol1 is selected in the Properties pane in visual studio and change the following properties:

    a. Set Appearance to Buttons

    b. Set ItemSize 0 for Width and 1 for Height

    c. Set Multiline to True

    d. Set SizeMode to Fixed

This is best done after your have finished your design time tasks as it hides them in the designer as well - making it difficult to navigate!

查看更多
登录 后发表回答