C# 包含 TabControl 的自动调整表单大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16498366/
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
Autosize Form containing TabControl
提问by Wasabi
I have a Windows Form with a bunch of Controls and it works just fine. All the Controls are contained within a TableLayoutPanelwhich autosizes according to the Controls base-size and the Formautosizes according to the panel, so I don't really have to worry about sizes in different platforms and computers since everything should resize according the the current configuration's settings.
我有一个带有一堆控件的 Windows 窗体,它工作得很好。所有控件都包含在一个TableLayoutPanel根据控件基本大小Form自动调整大小并根据面板自动调整大小的控件中,所以我真的不必担心不同平台和计算机的大小,因为一切都应该根据当前配置的设置调整大小.
For instance, here's a simple example of how it looks like. The relevant code is:
例如,这里有一个简单的示例,说明它的外观。相关代码是:
//...code defining all the other Commands and .Add()'ing them to the Panel
form.Controls.Add(Panel);
form.AutoSize = true;
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.ShowDialog();


I now want to get this entire panel and place it within a TabControl so that it is just one tab amonst many others. I did this as follows:
我现在想要获取整个面板并将其放置在 TabControl 中,以便它只是许多其他选项卡中的一个选项卡。我这样做如下:
//...code defining all the other Commands and .Add()'ing them to the Panel
TabControl tabControl = new TabControl();
tabControl.Dock = DockStyle.Fill;
TabPage tabPage = new TabPage("C1");
tabPage.Controls.Add(Panel);
tabControl.TabPages.Add(tabPage);
form.Controls.Add(tabControl);
form.AutoSize = true;
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.ShowDialog();
So basically, instead of adding the Panelto the Form, I add it to the TabPagewhich is added to the TabControlwhich is added to the Form. However, this results in:

所以基本上,我不是将Panel加到 中Form,而是将它TabPage加到加到 的TabControl加到 的加到Form. 但是,这会导致:

The documentation states that the AutoSizeproperty for TabControland TabPageis mere infrastructure with no relevance. Most of the "solutions" I've found suggest using .Dock = DockStyles.Fill, which helped in that the TabControlnow fills the Form as seen above instead of only occupying an even smaller part of it. The Form itself, however, remains unchanged.
该文件指出,AutoSize财产TabControl和TabPage与不相关单纯的基础设施。我发现的大多数“解决方案”都建议使用.Dock = DockStyles.Fill,这有助于TabControl现在填充表单,如上所示,而不是只占用其中更小的部分。然而,表格本身保持不变。
I've thought of using the Panel's size and making the TabControl's size equal to (or a function of) it, but I've noticed that the Sizeparameter apparently only changes when the Panelis painted, since adding Controls doesn't change it at all, so I'd have to wait for the Panelto paint and then resize it, which sounds sloppy. Is there a better solution?
我想过使用Panel's 大小并使TabControl's 大小等于(或函数)它,但我注意到Size参数显然只在Panel绘制时改变,因为添加控件不会改变它完全没有,所以我必须等待Panel它绘制然后调整它的大小,这听起来很草率。有更好的解决方案吗?
UPDATEUsing Anchoring and setting
使用锚定和设置更新
Panel.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
tabControl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
Simply lead to this:
简单地导致这个:
回答by Adrian
Instead of setting sizes, why not take advantage of Anchoring? If you anchor the Panelto Top | Left | Right | Bottomof the TabPage/TabControl, then the TabControlto the Formthe same way it's only ever the Form's size you need to worry about. It will dictate the sizes of all the other items anchored to it.
与其设置大小,不如利用Anchoring?如果锚定Panel到Top | Left | Right | Bottom的TabPage/ TabControl,那么TabControl在Form相同的方式,它只是有史以来最Form需要操心的大小。它将决定锚定到它的所有其他项目的大小。
回答by Andrey
use .AutoScaleMode = Nonein designer for your panel as usercontrol.
In my case TabControl-TabPage-myPage it works.
使用.AutoScaleMode = None在设计师为您的面板作为用户控件。在我的情况下 TabControl-TabPage-myPage 它工作。

