This question already has an answer here:
这个问题已经有了答案:
- How to place a label or a button exactly in the middle of the Form? 1 answer
- 如何将标签或按钮准确地放在窗体的中间?1回答
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.
出于兴趣,有没有一种非白痴的方式来做这件事?我真正想要的是类似于文本对齐的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.
目前,我正在将周围表单的填充属性设置为合适的大小,并将控件的Dock属性设置为fill。
9 个解决方案
#1
225
You could achieve this with the use of anchors. Or more precisely the non use of them.
您可以使用锚实现这一点。或者更确切地说,是不使用它们。
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中的properties窗口关闭锚定可能需要输入None, None(而不是默认的Top,Left)
#2
94
myControl.Left = (this.ClientSize.Width - myControl.Width) / 2 ;
myControl.Top = (this.ClientSize.Height - myControl.Height) / 2;
#3
32
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
选择控件->格式(菜单选项)-窗口中的>中心->水平或垂直
#4
8
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个列的列表。将中心列设置为绝对大小(无论您需要多少空间)。将两个外部列设置为100%。将一个面板添加到中心列,并添加任何您需要的控件,并将它们放置到您想要的位置。那个中心面板现在仍然以你的形式为中心。
#5
2
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.
您可以将您想要的控件放在面板中,并将左右填充值设置为大于默认值的值。只要它们是相等的,并且你的控件锚定在面板的侧面,那么它就会出现在面板的中心。然后可以根据需要将容器面板锚定到其父面板上。
#6
1
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).
它包括目测(我想你可以用计算器算一下),但只需在表单上插入said控件,然后删除锚定(anchor = None)。
#7
#8
0
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;
#9
0
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;
#1
225
You could achieve this with the use of anchors. Or more precisely the non use of them.
您可以使用锚实现这一点。或者更确切地说,是不使用它们。
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中的properties窗口关闭锚定可能需要输入None, None(而不是默认的Top,Left)
#2
94
myControl.Left = (this.ClientSize.Width - myControl.Width) / 2 ;
myControl.Top = (this.ClientSize.Height - myControl.Height) / 2;
#3
32
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
选择控件->格式(菜单选项)-窗口中的>中心->水平或垂直
#4
8
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个列的列表。将中心列设置为绝对大小(无论您需要多少空间)。将两个外部列设置为100%。将一个面板添加到中心列,并添加任何您需要的控件,并将它们放置到您想要的位置。那个中心面板现在仍然以你的形式为中心。
#5
2
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.
您可以将您想要的控件放在面板中,并将左右填充值设置为大于默认值的值。只要它们是相等的,并且你的控件锚定在面板的侧面,那么它就会出现在面板的中心。然后可以根据需要将容器面板锚定到其父面板上。
#6
1
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).
它包括目测(我想你可以用计算器算一下),但只需在表单上插入said控件,然后删除锚定(anchor = None)。
#7
1
To center Button in panel o in other container follow this step:
在其他容器的面板o中,按以下步骤将按钮居中:
- At design time set the position
- 在设计时设置位置。
- Go to properties Anchor of the button and set this value as the follow image
- 转到按钮的属性锚点,并将此值设置为如下图所示
#8
0
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;
#9
0
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;