How do I get a TabControl to use the full width of its parent?

49,124

Solution 1

Using the standard .NET tab control, this isn't directly possible. What is the ultimate goal for this? Are you trying to simulate the same type of tabbed-MDI style display as Visual Studio? If that's the case, there are several third-party solutions available - some open source and some commercial.

The other responses about using the Anchor property in combination with setting the size so it is just a bit larger than the actual window might work, but I think it might look a bit odd visually. It should work regardless of the theme and accessibility settings, but you may end up having to programmatically set the size to be a few pixels larger than the parent.

Solution 2

  1. Remove the height and width attributes from TabControl
  2. Set horizontal and vertical alignment to stretch

e.g. won't stretch;

<TabControl Height="373" Width="609" HorizontalAlignment="Stretch" Name="tabControl1" VerticalAlignment="Stretch"  VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">

e.g. will stretch;

<TabControl HorizontalAlignment="Stretch" Name="tabControl1" VerticalAlignment="Stretch"  VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">

Solution 3

Instead of using the Dock property you should try using the Anchor to anchor each of the four sides. Then you need to position the TabControl so it is positioned a couple of pixels bigger on all sides that the parent. That way the borders are hidden because they cannot be drawn when behind the parent control.

Solution 4

Is it possible to get a TabControl to display its TabPages in this manner, with no wasted screen space at the sides? If possible, I'd like to avoid solutions that involve painting the control myself.

If I understand your question correctly, and reading through the currently accepted answer, you want to know how to make the Tabs for a TabControl stretch across the whole Control with no wasted space like below:

To do this, set cTabControl.Dock = Fill, then make the following function and call it in Form1_Shown() and Form1_Resize(), or whatever "Resize()" functions you've created.

C#

void ResizeTabs()
{
    int numTabs = cTabControl.TabCount;

    float totLen = 0;
    using(Graphics g = CreateGraphics())
    {
        // Get total length of the text of each Tab name
        for(int i = 0; i < numTabs; i++)
            totLen += g.MeasureString(cTabControl.TabPages[i].Text, cTabControl.Font).Width;
    }

    int newX = (int)((cTabControl.Width - totLen) / numTabs) / 2;
    cTabControl.Padding = new Point(newX, cTabControl.Padding.Y);
}

VB

Sub ResizeTabs()
    Dim numTabs As Integer = cTabControl.TabCount

    Dim totLen As Decimal = 0
    Using g As Graphics = CreateGraphics()
        ' Get total length of the text of each Tab name
        For i As Integer = 0 To numTabs - 1
            totLen += g.MeasureString(cTabControl.TabPages(i).Text, cTabControl.Font).Width
        Next
    End Using

    Dim newX As Integer = ((cTabControl.Width - totLen) / numTabs) / 2
    cTabControl.Padding = New Point(newX, cTabControl.Padding.Y)
End Sub
Share:
49,124
Simon
Author by

Simon

Updated on July 09, 2022

Comments

  • Simon
    Simon almost 2 years

    The standard System.Windows.Forms.TabControl component draws a border around the TabPages it contains. If you set its Dock to Fill, these borders run up to the edge of the parent control, but they're still there, taking up screen space.

    In Visual Studio, if you dock two windows in the same place, you get a TabControl-like set of tabs along the bottom, but no borders along the sides.

    Is it possible to get a TabControl to display its TabPages in this manner, with no wasted screen space at the sides? If possible, I'd like to avoid solutions that involve painting the control myself.

    • Eric
      Eric almost 15 years
      Have you played around with the Margin property?
    • Stewbob
      Stewbob almost 15 years
      @Simon, You can use Phil Wright's solution and dynamically set the width of your TabControl at runtime to take into account different user settings.
    • Simon
      Simon over 14 years
      @Stewbob: If I have to do anything at runtime to take into account different user settings, then it's not the solution I'm after.
  • Simon
    Simon over 15 years
    Will that work identically irrespective of the user's theme and accessibility settings?
  • Simon
    Simon over 15 years
    Will that work identically irrespective of the user's theme and accessibility settings? It seems to me that if their theme is different, the distance beyond the edges might need to be different.
  • Tim Schmelter
    Tim Schmelter over 11 years
    There is no HorizontalAlignment property in TabControl, or am i missing something?
  • Tim Schmelter
    Tim Schmelter over 11 years
    You have realized that the question targets winforms?
  • Didier A.
    Didier A. over 10 years
    I think this answer is for WPF, not WinForms