Anchor of multiple buttons - C#

Hemanth B 886 Reputation points
2021-08-25T06:32:03.32+00:00

Hi I want my winforms app to be sizable. The controls should also size along with the form. So I have the following buttons: I set the anchor property to top, left, bottom and right for all buttons
126226-screenshot-2021-08-25-115543.png

But when I test it and increase the size of the form, this happens:
126243-screenshot-2021-08-25-115608.png

Please help. Also I want the buttons to automatically move to the bottom center of the form when the entire form is maximized

Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2021-08-25T09:00:12.123+00:00

    If it is like this :

    126284-buttons-resize.gif

    You could use a TableLayoutPanel or doing something like : (to be improved) =>

    private void Form1_Load(object sender, EventArgs e)  
    {              
       this.ClientSize = new System.Drawing.Size(600, 300);  
       int nX = 4;  
       int nY = 4;  
       int nWidth = ClientSize.Width / 9 - 4;  
       int nHeight= ClientSize.Height / 3 - 4;  
       for (int i = 1; i <= 26; i++)  
       {  
           Button button1 = new System.Windows.Forms.Button();  
           button1.Location = new System.Drawing.Point(nX, nY);  
           button1.Name = "button" + i;  
           button1.Size = new System.Drawing.Size(nWidth, nHeight);  
           button1.TabIndex = i;  
           button1.Text = Encoding.ASCII.GetString(new byte[] { (byte)(i + 64) });  
           button1.UseVisualStyleBackColor = true;  
           Controls.Add(button1);  
           nX += nWidth + 4;  
           if (i % 9 == 0)  
           {  
               if (i < 18)  
                   nX = 4;  
               else  
                   nX = 4 + nWidth / 2;  
               nY += nHeight + 4;  
           }  
       }  
       CenterToScreen();                       
    }  
      
    protected override void OnResize(EventArgs e)  
    {  
        base.OnResize(e);  
      
        int nX = 4;  
        int nY = 4;  
        int nWidth = ClientSize.Width / 9 - 4;  
        int nHeight = ClientSize.Height / 3 - 4;  
        int i = 1;  
        foreach (Control item in this.Controls)  
        {  
            item.Location = new System.Drawing.Point(nX, nY);  
            item.Size = new System.Drawing.Size(nWidth, nHeight);  
            nX += nWidth + 4;  
            if (i % 9 == 0)  
            {  
                if (i < 18)  
                    nX = 4;  
                else  
                    nX = 4 + nWidth/2;  
                nY += nHeight + 4;  
            }  
            i++;  
        }  
    }  
    
    0 comments No comments

  2. Viorel 122.5K Reputation points
    2021-08-25T09:21:26.197+00:00

    If you are interested in automatic resizing (without code), then consider two TabLayoutPanel controls.

    The first one will have nine columns and two rows. In Properties window for rows and columns, specify percent: 11,11% for columns, 50,00% for rows. This panel will hold the first two rows of your buttons.

    The second one will have ten columns and a single row. Specify 5,56% for the first and last columns, and 11,11% for the other columns. The first and last cells will be empty.

    All the buttons will be placed inside the table's cells and will be docked (Dock=Fill).

    Place and resize the panels manually inside the form and specify Anchor=Bottom,Left,Right.

    You can develop a separate User Control, especially if you want to use it in different forms.

    If you actually need a fixed or minimal width, you can still use TabLayoutPanel. Show some details.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.