How do I automatically move and resize buttons and labels when a form is expanded?

Viewed 12248

So I am using a form application that has buttons and labels in it (Picture) and I want to know how to make them re-size properly when I maximize the form. The anchor or/and dock property as in these questions: C# form auto resize window or Automatic resizing of the Windows Forms controls does not help because it expands only 1 item.

I need it to move and resize without going out of the original proportions. So like let's say I have 2 buttons above each other. I want them to stay always on the right side and they are each 10*12 pixels and 2 pixels from edge. When I expand the original window size three times, each button will still be 2 pixels from the edge but 30*36 pixels big.

3 Answers

This is an example code for 12 buttons (4x3). It also changes the font size compared to the button size.

  1. Open a windows form
  2. Create 12 buttons
  3. Use the code below:

    public Form1() { InitializeComponent(); SetComponents(); }

    void SetComponents()
    {
        foreach (Control item in Controls)
        {
            if (item is Button)
            {
                item.Width = Size.Width / 4 - 16;
                item.Height = Size.Height / 4;
                item.Font = new Font(Font.FontFamily, item.Height / 5);
            }
    
            button1.Left = 10;
            button1.Top = 10;
    
            button2.Left = button1.Right + 10;
            button2.Top = button1.Top;
    
            button3.Left = button2.Right + 10;
            button3.Top = button2.Top;
    
            button4.Left = button3.Right + 10;
            button4.Top = button3.Top;
    
            button5.Left = button1.Left;
            button5.Top = button1.Bottom + 10;
    
            button6.Left = button5.Right + 10;
            button6.Top = button2.Bottom + 10;
    
            button7.Left = button6.Right + 10;
            button7.Top = button3.Bottom + 10;
    
            button8.Left = button7.Right + 10;
            button8.Top = button4.Bottom + 10;
    
            button9.Left = button1.Left;
            button9.Top = button5.Bottom + 10;
    
            button10.Left = button9.Right + 10;
            button10.Top = button6.Bottom + 10;
    
            button11.Left = button10.Right + 10;
            button11.Top = button7.Bottom + 10;
    
            button12.Left = button11.Right + 10;
            button12.Top = button8.Bottom + 10;
    
        }
    }
    
    private void Form1_Resize(object sender, EventArgs e)
    {
        SetComponents();
    }
    
Related