C# 如何让 TabControl 使用其父级的全宽?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/491061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get a TabControl to use the full width of its parent?
提问by Simon
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.
标准 System.Windows.Forms.TabControl 组件在它包含的 TabPage 周围绘制一个边框。如果你将它的 Dock 设置为 Fill,这些边框会一直延伸到父控件的边缘,但它们仍然存在,占用屏幕空间。
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.
在 Visual Studio 中,如果将两个窗口停靠在同一位置,则会在底部获得一组类似于 TabControl 的选项卡,但两侧没有边框。
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.
是否有可能让 TabControl 以这种方式显示其 TabPages,而不会浪费侧面的屏幕空间?如果可能,我想避免涉及自己绘制控件的解决方案。
采纳答案by Scott Dorman
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.
使用标准的 .NET 选项卡控件,这不是直接可能的。这样做的最终目标是什么?您是否正在尝试模拟与 Visual Studio 相同类型的选项卡式 MDI 样式显示?如果是这种情况,有几种第三方解决方案可用——一些是开源的,一些是商业的。
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.
关于将 Anchor 属性与设置大小结合使用的其他响应,使其仅比实际窗口大一点可能会起作用,但我认为它在视觉上可能看起来有点奇怪。无论主题和辅助功能设置如何,它都应该可以工作,但您最终可能不得不以编程方式将大小设置为比父级大几个像素。
回答by Phil Wright
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.
您应该尝试使用 Anchor 来锚定四个边中的每一个,而不是使用 Dock 属性。然后您需要定位 TabControl,使其在所有边上都比父级大几个像素。这样边框就被隐藏了,因为它们在父控件后面时无法绘制。
回答by Pat
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;
childControl.Anchor = Anchor.Left|Anchor.Right; childControl.Width = parentControl.Width;
回答by Pat
Do not Dock the TabControl. Stretch it out on the designer so its left and right edges extend beyond the window.
不要停靠 TabControl。在设计器上将其拉伸,使其左右边缘延伸到窗口之外。
回答by rajchanchal
<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>
回答by Daniel de Zwaan
- Remove the height and width attributes from TabControl
- Set horizontal and vertical alignment to stretch
- 从 TabControl 中删除高度和宽度属性
- 设置水平和垂直对齐以拉伸
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">
回答by DCOPTimDowd
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.
是否有可能让 TabControl 以这种方式显示其 TabPages,而不会浪费侧面的屏幕空间?如果可能,我想避免涉及自己绘制控件的解决方案。
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 spacelike below:
如果我正确理解了您的问题,并通读了当前接受的答案,您想知道如何使 TabControl 的 Tabs 延伸到整个 Control 中,而不会浪费空间,如下所示:
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.
为此,请设置cTabControl.Dock = Fill,然后创建以下函数并在Form1_Shown()和Form1_Resize()或您创建的任何“Resize()”函数中调用它。
C#
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
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