简体   繁体   中英

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

I am trying to make a button's size and location change when the user resizes a form.

How can I assigning a size to a button?

I tried to make it with changing width and height separately. I know that I can make it by anchoring it, but I would like to make it with pure coding.
Also refreshing the form did not work. I can set the locations of buttons easily with the Location property, but the size property does not work. I couldn't find the difference...

Here is the full code which works for changing the positions of objects, but does not work for changing the 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();

what's wrong with setting the Alignment property to suit your needs?

you can also put it inside a 3x3 Table Layout Panel that will be docked / aligned correctly...

let winforms work it out ;)

对于仍在寻找此问题答案的用户,请记住在尝试以编程方式更改按钮大小时将按钮的Behaviour:Autosize属性设置为FALSE。

You need to attach an event handler for the Resize event of your current Form. Then inside this event handler, which is just another method, you can then resize the button, or do whatever you need to do when the form is resized.

I think you need to first get a better understanding of how event handling works in Windows Forms. Read here for more - http://msdn.microsoft.com/en-us/library/aa983610%28VS.71%29.aspx

UPDATE: OK I see that you have already attached the event handler. I missed this, and assumed you weren't aware of how to do this.

Make sure that the resize event is still attached to the event handler method you've shown us in your answer, and then it should just work, unless you are working with multiple threads, or BackgroundWorker instances. Updating the user interface from a different thread to that of the main UI thread needs to be done differently, and with care.

I couldn't manage to find it out, why it does not change it with my code while it works with Jamie's code. But instead of working on that, i created 9 buttons with pure code. So it gave me ability to change every property.

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

}
}

I don't know if someone needs the code, i just pasted.

From the docs.microsoft.com documentation for the Control.Size property:

Because the Size class is a value type (Structure in Visual Basic, struct in Visual C#), it is returned by value, meaning accessing the property returns a copy of the size of the control. So, adjusting the Width or Height properties of the Size returned from this property will not affect the Width or Height of the control. To adjust the Width or Height of the control, you must set the control's Width or Height property, or set the Size property with a new Size.

and

To maintain better performance, do not set the Size of a control in its constructor. The preferred method is to override the DefaultSize property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM