我有一个面板,在其中试图将控件居中。但显然,面板停靠在控件边缘时不喜欢填充。这是我的面板的当前代码:
buttonPanel = new Panel();
buttonPanel.Width = 300;
buttonPanel.Height = 50;
buttonPanel.Dock = DockStyle.Bottom;
buttonPanel.Padding = new Padding((this.ClientSize.Width - buttonPanel.Width) / 2, 0,
(this.ClientSize.Width - buttonPanel.Width) / 2, 0);
buttonPanel.BackColor = Color.Transparent;
this.Controls.Add(buttonPanel);
我在面板内放置了一个按钮。我希望上面的代码能够将控件很好地放置在面板中心300宽的“矩形”的左侧。但是该按钮位于最左侧,就像填充被忽略一样:
如何将按钮集合居中放置在表单中心?
设计时间方法
在设计时,将按钮放入容器中,然后选择按钮。然后,使用水平居中和垂直居中从布局工具栏把你的按钮在面板的中心。之后,转到按钮属性,然后从“锚点”属性中删除每个锚点。
编码方式
Panel panel = new Panel();
panel.Size = new Size(300, 100);
panel.Dock = DockStyle.Bottom;
Button button = new Button();
button.Size = new Size(70, 25);
button.Location = new Point((panel.Width - button.Width) / 2, (panel.Height - button.Height) / 2);
button.Anchor = AnchorStyles.None;
panel.Controls.Add(button);
this.Controls.Add(panel);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句