【发布时间】:2010-07-09 20:22:33
【问题描述】:
int width, height;
width = this.Size.Width;
height = this.Size.Height;
width /= 3;
height /= 3;
btn_1.Size = new Size(width, height);
我正在尝试在用户调整表单大小时更改按钮的大小和位置。
如何为按钮指定大小?
我试图通过分别改变宽度和高度来实现它。我知道我可以通过锚定它来制作它,但我想用纯编码来制作它。
刷新表单也不起作用。我可以使用Location 属性轻松设置按钮的位置,但 size 属性不起作用。我找不到区别...
这是用于更改对象位置但不适用于更改大小的完整代码:
private void form_counterMain_Resize(object sender, EventArgs e)
{
int width, height;
Point templocation;
templocation = new Point(0, 0);
width = this.Size.Width;
height = this.Size.Height;
width /= 3;
height /= 3;
//:::location:::
btn_1.Location = templocation;
templocation.X = width;
btn_2.Location = templocation;
templocation.X = width * 2;
btn_3.Location = templocation;
templocation.X = 0;
templocation.Y = height;
btn_4.Location = templocation;
templocation.X = width;
btn_5.Location = templocation;
templocation.X = width * 2;
btn_6.Location = templocation;
templocation.Y = height * 2;
templocation.X = 0;
btn_7.Location = templocation;
templocation.X = width;
btn_8.Location = templocation;
templocation.X = width * 2;
btn_9.Location = templocation;
//:::size:::
btn_1.Size = new Size(width, height);
this.Refresh();
【问题讨论】:
-
您是否尝试过设置 .width 和 .height 而不是 .size?
-
另外,编辑大小后尝试刷新论坛。
-
@Meiscooldude,您可以使用 Application.DoEvents(); 进行此刷新事件;
-
即使你让它工作,当你的表单被调整大小时,你通常不应该手动调整按钮或任何其他控件的大小。相反,请尝试以特定方式停靠/锚定您的按钮以获得您想要的。
-
它们都没有工作,而且我不想使用任何锚定或对接,它们不能以我想要的方式工作