C# 在 .NET (Winforms) 中的窗体中居中控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/491399/
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
Centering controls within a form in .NET (Winforms)?
提问by user47741
I'm trying to center a fixed size control within a form.
我试图在表单中居中固定大小的控件。
Out of interest, is there a non-idiotic way of doing this? What I really want is something analogous to the text-align css property.
出于兴趣,有没有一种非愚蠢的方法来做到这一点?我真正想要的是类似于 text-align css 属性的东西。
At the moment, I'm setting the padding property of the surrounding form to a suitable size and setting the Dock property of the control to fill.
目前,我正在将周围表单的 padding 属性设置为合适的大小,并将控件的 Dock 属性设置为填充。
回答by splattne
You could achieve this with the use of anchors. Or more precisely the non use of them.
您可以通过使用anchors来实现这一点。或者更准确地说是不使用它们。
Controls are anchored by default to the top left of the form which means when the form size will be changed, their distance from the top left side of the form will remain constant. If you change the control anchor to bottom left, then the control will keep the same distance from the bottom and left sides of the form when the form if resized.
默认情况下,控件锚定在窗体的左上角,这意味着当窗体大小更改时,它们与窗体左上角的距离将保持不变。如果将控件锚点更改为左下角,则在调整窗体大小时,控件将与窗体底部和左侧保持相同的距离。
Turning off the anchor in a direction will keep the control centered in that direction when resizing.
在调整大小时,关闭某个方向的锚点将使控件保持在该方向的中心。
NOTE:Turning off anchoring via the properties window in VS2015 may require entering None, None (instead of default Top,Left)
注意:通过 VS2015 中的属性窗口关闭锚定可能需要输入 None、None(而不是默认的 Top、Left)
回答by Mitch Wheat
myControl.Left = (this.ClientSize.Width - myControl.Width) / 2 ;
myControl.Top = (this.ClientSize.Height - myControl.Height) / 2;
回答by t3rse
It involves eyeballing it (well I suppose you could get out a calculator and calculate) but just insert said control on the form and then remove any anchoring (anchor = None).
它涉及观察它(我想你可以拿出一个计算器并计算),但只需在表单上插入所述控件,然后删除任何锚定(锚点 = 无)。
回答by Robert MacLean
Since you don't state if the form can resize or not there is an easy way if you don't care about resizing (if you do care, go with Mitch Wheats solution):
由于您没有说明表单是否可以调整大小,如果您不关心调整大小,则有一种简单的方法(如果您确实关心,请使用 Mitch Wheats 解决方案):
Select the control -> Format (menu option) -> Center in Window -> Horizontally or Vertically
选择控件 -> 格式(菜单选项) -> 窗口居中 -> 水平或垂直
回答by Chad
I found a great way to do this and it will work with multiple controls. Add a TableLayout with 3 columns. Make the center column an absolute size (however much room you need). Set the two outside columns to 100%. Add a Panel to the center column and add any controls you need and place them where you want. That center panel will now remain centered in your form.
我找到了一个很好的方法来做到这一点,它可以与多个控件一起使用。添加一个包含 3 列的 TableLayout。使中心列成为绝对大小(无论您需要多少空间)。将两个外部列设置为 100%。将面板添加到中心列并添加您需要的任何控件并将它们放置在您想要的位置。该中心面板现在将在您的表单中保持居中。
回答by Droj
You can put the control you want to center inside a Panel and set the left and right padding values to something larger than the default. As long as they are equal and your control is anchored to the sides of the Panel, then it will appear centered in that Panel. Then you can anchor the container Panel to its parent as needed.
您可以将要居中的控件放在 Panel 中,并将左右填充值设置为大于默认值的值。只要它们相等并且您的控件锚定在面板的两侧,那么它就会出现在该面板的中心。然后,您可以根据需要将容器面板锚定到其父级。
回答by daniele3004
回答by Ahad aghapour
you can put all your controls to panel and then write a code to move your panel to center of your form.
您可以将所有控件放在面板上,然后编写代码将面板移动到表单的中心。
panelMain.Location =
new Point(ClientSize.Width / 2 - panelMain.Size.Width / 2,
ClientSize.Height / 2 - panelMain.Size.Height / 2);
panelMain.Anchor = AnchorStyles.None;
回答by M. Fawad Surosh
In addition, if you want to align it to the center of another control:
此外,如果要将其与另一个控件的中心对齐:
//The "ctrlParent" is the one on which you want to align "ctrlToCenter".
//"ctrlParent" can be your "form name" or any other control such as "grid name" and etc.
ctrlToCenter.Parent = ctrlParent;
ctrlToCenter.Left = (ctrlToCenter.Parent.Width - ctrlToCenter.Width) / 2;
ctrlToCenter.Top = (ctrlToCenter.Parent.Height - ctrlToCenter.Height) / 2;