[英]How to change a button's size
int width, height;
width = this.Size.Width;
height = this.Size.Height;
width /= 3;
height /= 3;
btn_1.Size = new Size(width, height);
我正在尝试在用户调整表单大小时更改按钮的大小和位置。
如何为按钮分配大小?
我试图通过分别更改宽度和高度来实现。 我知道我可以通过锚定来实现,但是我想通过纯编码来实现。
另外刷新表格也没有用。 我可以使用Location
属性轻松设置按钮的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();
设置Alignment属性以满足您的需求有什么问题?
您也可以将其放在3x3表格布局面板中,该面板将正确对接/对齐...
让winforms解决它;)
对于仍在寻找此问题答案的用户,请记住在尝试以编程方式更改按钮大小时将按钮的Behaviour:Autosize属性设置为FALSE。
您需要为当前窗体的Resize事件附加事件处理程序 。 然后,在该事件处理程序内(这是另一种方法),您可以调整按钮的大小,或者执行调整表单大小时需要做的任何事情。
我认为您首先需要更好地了解Windows窗体中事件处理的工作方式。 在这里阅读更多-http://msdn.microsoft.com/zh-cn/library/aa983610%28VS.71%29.aspx
更新:好的,我看到您已经附加了事件处理程序。 我错过了,并假设您不知道如何执行此操作。
确保将resize事件仍附加到您在答案中向我们显示的事件处理程序方法,然后它应该可以正常工作,除非您正在使用多个线程或BackgroundWorker实例。 将用户界面从其他线程升级到主UI线程的操作需要以不同的方式进行,并且要小心。
我无法找出原因,为什么它在与Jamie的代码一起工作时却没有用我的代码对其进行更改。 但是我没有做这些工作,而是用纯代码创建了9个按钮。 因此,它使我能够更改每个属性。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace hw02
{
public partial class form_counterMain : Form
{
int[] b=new int[9]; //initialized the counters
Button[] btn= new Button[9]; //initialized the buttons
public form_counterMain()
{
for (int t = 0; t < 9; t++) //this loop makes all the counters 0
{
b[t] = 0;
}
for (int t = 0; t < 9;t++) //this loop makes all the buttons assigned to a button
{
btn[t]=new Button();
}
InitializeComponent();
changeFunc(); //first calculation
btn[0].Click += new System.EventHandler(btn0Click); //here i assign the functions to buttons
btn[1].Click += new System.EventHandler(btn1Click);
btn[2].Click += new System.EventHandler(btn2Click);
btn[3].Click += new System.EventHandler(btn3Click);
btn[4].Click += new System.EventHandler(btn4Click);
btn[5].Click += new System.EventHandler(btn5Click);
btn[6].Click += new System.EventHandler(btn6Click);
btn[7].Click += new System.EventHandler(btn7Click);
btn[8].Click += new System.EventHandler(btn8Click);
}
private void form_counterMain_Resize(object sender, EventArgs e)
{
changeFunc();
}
private void changeFunc()
{
int width, height;
Point templocation = new Point(0, 0);
width = this.Size.Width;
height = this.Size.Height;
width = width/3 -5; //here i calculated the best values for 3 buttons
height = height/3-12;
for (int i = 0; i < 9; i++) //here i assign some necessary values to buttons and read the count numbers from memory
{
btn[i].Name = "btn_" + i; //the names are changed!
btn[i].TabIndex = i;
btn[i].Text = b[i].ToString();
btn[i].Size = new Size(width, height);
btn[i].Visible = true;
btn[i].Parent = this;
btn[i].FlatStyle = System.Windows.Forms.FlatStyle.Flat;
}
//this lines sets the location of the buttons
btn[0].Location = templocation;
templocation.X = width;
btn[1].Location = templocation;
templocation.X = width * 2;
btn[2].Location = templocation;
templocation.X = 0;
templocation.Y = height;
btn[3].Location = templocation;
templocation.X = width;
btn[4].Location = templocation;
templocation.X = width * 2;
btn[5].Location = templocation;
templocation.Y = height * 2;
templocation.X = 0;
btn[6].Location = templocation;
templocation.X = width;
btn[7].Location = templocation;
templocation.X = width * 2;
btn[8].Location = templocation;
}
//here the functions start, they only increase the integers in the memory and then they force the program to refresh its visual state
private void btn0Click(Object sender, EventArgs e)
{
b[0]++;
changeFunc();
}
private void btn1Click(Object sender, EventArgs e)
{
b[1]++;
changeFunc();
}
private void btn2Click(Object sender, EventArgs e)
{
b[2]++;
changeFunc();
}
private void btn3Click(Object sender, EventArgs e)
{
b[3]++;
changeFunc();
}
private void btn4Click(Object sender, EventArgs e)
{
b[4]++;
changeFunc();
}
private void btn5Click(Object sender, EventArgs e)
{
b[5]++;
changeFunc();
}
private void btn6Click(Object sender, EventArgs e)
{
b[6]++;
changeFunc();
}
private void btn7Click(Object sender, EventArgs e)
{
b[7]++;
changeFunc();
}
private void btn8Click(Object sender, EventArgs e)
{
b[8]++;
changeFunc();
}
}
}
我不知道有人是否需要代码,我只是粘贴了。
从docs.microsoft.com文档获取Control.Size
属性:
因为Size类是一个值类型(Visual Basic中的结构,Visual C#中的struct),所以它由值返回,这意味着访问该属性将返回控件大小的副本。 因此,调整从此属性返回的Size的Width或Height属性不会影响控件的Width或Height。 若要调整控件的宽度或高度,必须设置控件的Width或Height属性,或使用新的Size设置Size属性。
和
为了保持更好的性能,请不要在其构造函数中设置控件的大小。 首选方法是重写DefaultSize属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.