如何让 TabControl 使用其父控件的完整宽度?

发布于 2024-07-13 02:02:44 字数 286 浏览 8 评论 0原文

标准 System.Windows.Forms.TabControl 组件在它包含的 TabPage 周围绘制边框。 如果将其 Dock 设置为 Fill,这些边框会延伸到父控件的边缘,但它们仍然存在,占用屏幕空间。

在 Visual Studio 中,如果将两个窗口停靠在同一位置,则会在底部看到一组类似于 TabControl 的选项卡,但两侧没有边框。

是否有可能让 TabControl 以这种方式显示其 TabPages,而不会浪费两侧的屏幕空间? 如果可能的话,我想避免涉及自己绘制控件的解决方案。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

七月上 2024-07-20 02:02:44
  1. 从 TabControl 中删除高度和宽度属性
  2. 设置水平和垂直对齐方式以拉伸

,例如 不会伸展;

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

例如会伸展;

<TabControl HorizontalAlignment="Stretch" Name="tabControl1" VerticalAlignment="Stretch"  VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
  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">
携君以终年 2024-07-20 02:02:44

您应该尝试使用 Anchor 来锚定四个边,而不是使用 Dock 属性。 然后,您需要定位 TabControl,使其在所有边上都比父控件大几个像素。 这样,边框就会被隐藏,因为它们在父控件后面时无法绘制。

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.

攀登最高峰 2024-07-20 02:02:44

使用标准 .NET 选项卡控件,这是不可能直接实现的。 这样做的最终目标是什么? 您是否正在尝试模拟与 Visual Studio 相同类型的选项卡式 MDI 样式显示? 如果是这种情况,有几种可用的第三方解决方案 - 一些是开源的,一些是商业的。

关于使用 Anchor 属性并设置大小使其比实际窗口稍大一点的其他响应可能会起作用,但我认为它在视觉上看起来可能有点奇怪。 无论主题和辅助功能设置如何,它都应该有效,但您最终可能必须以编程方式将大小设置为比父级大几个像素。

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.

掐死时间 2024-07-20 02:02:44

是否有可能让 TabControl 以这种方式显示其 TabPage,而不会浪费两侧的屏幕空间? 如果可能的话,我想避免涉及自己绘制控件的解决方案。

如果我正确理解您的问题,并通读当前接受的答案,您想知道如何为TabControl 延伸到整个控件没有浪费空间,如下所示:

为此,请设置cTabControl.Dock = Fill,然后创建以下函数并调用它在 Form1_Shown()Form1_Resize() 中,或者您创建的任何“Resize()”函数中。

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

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
相守太难 2024-07-20 02:02:44

我通过在选项卡标题中添加空格解决了同样的问题:

var pageAlignment = 50;

TabPage1.Text = TabPage1.Text.PadLeft(pageAlignment / 2).PadRight(pageAlignment);
TabPage2.Text = TabPage2.Text.PadLeft(pageAlignment / 2).PadRight(pageAlignment);
TabPage3.Text = TabPage3.Text.PadLeft(pageAlignment / 2).PadRight(pageAlignment);

I solved the same problem by adding spaces to the tabs title:

var pageAlignment = 50;

TabPage1.Text = TabPage1.Text.PadLeft(pageAlignment / 2).PadRight(pageAlignment);
TabPage2.Text = TabPage2.Text.PadLeft(pageAlignment / 2).PadRight(pageAlignment);
TabPage3.Text = TabPage3.Text.PadLeft(pageAlignment / 2).PadRight(pageAlignment);

樱娆 2024-07-20 02:02:44

锚定控件的左侧和右侧,并将宽度设置为父控件的宽度。

childControl.Anchor = Anchor.Left|Anchor.Right;
子控件.宽度 = 父控件.宽度;

Anchor the left and right sides of the control with the width set to the width of the parent control.

childControl.Anchor = Anchor.Left|Anchor.Right;
childControl.Width = parentControl.Width;

烟雨凡馨 2024-07-20 02:02:44
<Grid>
   <TabControl Name="tabControl1" >
       <TabItem Header="tabItem1" Name="tabItem1">
          <Grid />
       </TabItem>
       <TabItem Header="tabItem2" Name="tabItem2">
          <Grid />
       </TabItem>
       <TabItem Header="tabItem3" Name="tabItem3">
          <Grid />
       </TabItem>
  </TabControl>
</Grid>
<Grid>
   <TabControl Name="tabControl1" >
       <TabItem Header="tabItem1" Name="tabItem1">
          <Grid />
       </TabItem>
       <TabItem Header="tabItem2" Name="tabItem2">
          <Grid />
       </TabItem>
       <TabItem Header="tabItem3" Name="tabItem3">
          <Grid />
       </TabItem>
  </TabControl>
</Grid>
涙—继续流 2024-07-20 02:02:44

不要停靠 TabControl。 将其拉伸到设计器上,使其左右边缘延伸到窗口之外。

Do not Dock the TabControl. Stretch it out on the designer so its left and right edges extend beyond the window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文