如何让 TabControl 使用其父控件的完整宽度?
标准 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
,例如 不会伸展;
例如会伸展;
e.g. won't stretch;
e.g. will stretch;
您应该尝试使用 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.
使用标准 .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.
如果我正确理解您的问题,并通读当前接受的答案,您想知道如何为TabControl 延伸到整个控件没有浪费空间,如下所示:
为此,请设置cTabControl.Dock = Fill,然后创建以下函数并调用它在 Form1_Shown() 和 Form1_Resize() 中,或者您创建的任何“Resize()”函数中。
C#
VB
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#
VB
我通过在选项卡标题中添加空格解决了同样的问题:
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);
锚定控件的左侧和右侧,并将宽度设置为父控件的宽度。
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;
不要停靠 TabControl。 将其拉伸到设计器上,使其左右边缘延伸到窗口之外。
Do not Dock the TabControl. Stretch it out on the designer so its left and right edges extend beyond the window.