如何更改按钮的大小

时间:2010-07-09 20:22:34

标签: c# winforms button

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();

5 个答案:

答案 0 :(得分:2)

设置Alignment属性以满足您的需求有什么问题?

你也可以把它放在一个3x3表格布局面板中,它将正确停靠/对齐......

让winforms解决它;)

答案 1 :(得分:1)

对于仍在搜索此问题答案的用户,请记住在尝试以编程方式更改其大小时,将按钮的行为:自动调整大小属性设置为FALSE。

答案 2 :(得分:0)

您需要为当前表单的调整大小事件附加事件处理程序。然后在这个事件处理程序中,这只是另一种方法,然后你可以调整按钮的大小,或者在调整窗体大小时做你需要做的任何事情。

我认为您需要首先更好地了解事件处理在Windows窗体中的工作原理。请阅读此处了解更多信息 - http://msdn.microsoft.com/en-us/library/aa983610%28VS.71%29.aspx

更新:确定我看到您已经附加了事件处理程序。我错过了这个,并假设你不知道该怎么做。

确保resize事件仍附加到您在答案中向我们展示的事件处理程序方法,然后它应该正常工作,除非您使用多个线程或BackgroundWorker实例。将用户界面从不同的线程更新为主UI线程的用户界面需要以不同的方式完成,并且要小心。

答案 3 :(得分:0)

我无法找到它,为什么它不会改变我的代码,而它与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();
    }

}
}

我不知道是否有人需要代码,我只是粘贴了。

答案 4 :(得分:0)

来自Control.Size属性的docs.microsoft.com documentation

  

因为Size类是值类型(Visual Basic中的结构,   在Visual C#中的struct,它是按值返回的,意味着访问   property返回控件大小的副本。所以,调整   从此属性返回的Size的Width或Height属性   不会影响控件的宽度或高度。要调整   控件的宽度或高度,您必须设置控件的宽度或   高度属性,或使用新大小设置Size属性。

  

要保持更好的性能,请不要设置控件的大小   它的构造函数。首选方法是覆盖DefaultSize   属性。