resize entire form automatic

Jordan Halgunseth 1 Reputation point
2022-12-06T17:58:03.687+00:00

Is there a way to resize entire form with picture box . so if form is 50% picture box is 50%

Developer technologies Windows Forms
Developer technologies .NET Other
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-12-07T01:50:31.797+00:00

    Hi @Jordan Halgunseth ,
    You can refer to the following code to resize the controls' size according to the scaling ratio of the form.

            public Form1()  
            {  
                InitializeComponent();  
                x = this.Width;  
                y = this.Height;  
                setTag(this);  
            }  
            private float x;  
            private float y;  
            private void setTag(Control cons)  
            {  
                foreach (Control con in cons.Controls)  
                {  
                    con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;  
                    if (con.Controls.Count > 0)  
                    {  
                        setTag(con);  
                    }  
                }  
            }  
            private void setControls(float newx, float newy, Control cons)  
            {  
                foreach (Control con in cons.Controls)  
                {  
                    if (con.Tag != null)  
                    {  
                        string[] mytag = con.Tag.ToString().Split(new char[] { ';' });  
                        con.Width = Convert.ToInt32(System.Convert.ToSingle(mytag[0]) * newx);  
                        con.Height = Convert.ToInt32(System.Convert.ToSingle(mytag[1]) * newy);  
                        con.Left = Convert.ToInt32(System.Convert.ToSingle(mytag[2]) * newx);  
                        con.Top = Convert.ToInt32(System.Convert.ToSingle(mytag[3]) * newy);  
                        Single currentSize = System.Convert.ToSingle(mytag[4]) * newy;  
                        con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);  
                        if (con.Controls.Count > 0)  
                        {  
                            setControls(newx, newy, con);  
                        }  
                    }  
                }  
            }  
            private void Form1_ResizeEnd(object sender, EventArgs e)  
            {  
                float newx = (this.Width) / x;  
                float newy = (this.Height) / y;  
                setControls(newx, newy, this);  
            }  
    

    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Reza Aghaei 4,986 Reputation points MVP Volunteer Moderator
    2022-12-28T22:51:59.623+00:00

    Are you looking for a TableLayoutPanel?

    In scenarios that you want a control occupy a percentage of its container, or for dynamic layouts, or for a mix between percentage, fixed size, or auto size, you may want to use TableLayoutPanel.

    You can use it in designer, and define Rows and Columns. Or you can use it in the code, like the following example:

    var f = new Form();  
    
    var tableLayoutPanel1 = new TableLayoutPanel();  
    tableLayoutPanel1.Name = "tableLayoutPanel1";  
    tableLayoutPanel1.RowCount = 1;  
    tableLayoutPanel1.ColumnCount = 2;  
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));  
    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));  
    tableLayoutPanel1.Dock = DockStyle.Fill;  
    
    var pictureBox1 = new PictureBox();  
    pictureBox1.Margin = new Padding(5);  
    pictureBox1.Name = "pictureBox1";  
    pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;  
    pictureBox1.ImageLocation = "https://loremflickr.com/320/240";  
    pictureBox1.Dock = DockStyle.Fill;  
    
    
    var pictureBox2 = new PictureBox();  
    pictureBox2.Margin = new Padding(5);  
    pictureBox2.Name = "pictureBox1";  
    pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;  
    pictureBox2.ImageLocation = "https://loremflickr.com/320/240";  
    pictureBox2.Dock = DockStyle.Fill;  
    
    
    tableLayoutPanel1.Controls.Add(pictureBox1);  
    tableLayoutPanel1.Controls.Add(pictureBox2);  
    
    f.Controls.Add(tableLayoutPanel1);  
    f.Show();  
    

    Which creates the following form:

    274683-image.png

    And when you resize it, you see each image has occupied 50% of the form:

    274632-image.png

    0 comments No comments

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.