views:

91

answers:

3
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 user resized a form.

How can I assigning a size to a button?

Also yes i tried to make it with changing width and height separately. I know that i can make it with anchoring it but i want to make it with pure coding Also refreshing did not work. I can set the locations of buttons easily with Location property but size property does not work, i couldnt find the difference

Full code which works for chanhing the positions but not works 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();
A: 

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.

Saajid Ismail
yes definitely it should work that way, but it won't :D . That the problem, this was our homework neither me nor my friends could managed to do it that way. Thanks anyway
gkaykck
A: 

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

Kaneti
A: 

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.

gkaykck
Set a breakpoint inside the Form_Resize() event handler, and check what is happening. It could be that the event isn't event firing, due to the form not actually resizing. One of the properties of the Form could be forcing the form to maintain a fixed size. Or, one of the prooperties of the button could cause it to maintain a fixed size.
Saajid Ismail