How to expand & reduce panel?

LooBi 141 Reputation points
2022-05-11T00:58:49.893+00:00

![200710-image.png

I have two panels in tabcontrol.
I want to make panel B reduce when I expand panel A.
and panel A reduce when I expand panel B.
so I mean how to adjust two panels?!
Thank you!


Edit------------
I would like to move like this.

200756-ezgifcom-gif-maker.gif

Developer technologies | Windows Forms
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2022-05-11T02:34:20.433+00:00

    Instead of two panels, insert a SplitContainer control and change its Orientation property to "Horizontal".


1 additional answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-05-11T02:15:39.36+00:00

    @LooBi , Welcome to Microsoft Q&A, you could adjuest two panel's size and location to expand or reduce the two panels.

    Here is a code example you could refer to.

       Panel panel1 = new Panel();  
            Panel panel2 = new Panel();  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                
                panel1.BackColor = Color.Blue;  
                panel1.Size=new Size(tabPage1.Width,tabPage1.Height/2);  
                panel1.Click += Panel1_Click;  
                TextBox textBox1 = new TextBox();  
                textBox1.Text = "Hello";  
                panel1.Controls.Add(textBox1);  
      
                panel2.BackColor = Color.Yellow;  
                panel2.Size = new Size(tabPage1.Width, tabPage1.Height / 2);  
                panel2.Location = new Point(panel1.Left, panel1.Bottom);  
                panel2.Click += Panel2_Click;  
                CheckBox checkBox1 = new CheckBox();  
                checkBox1.Text = "Do you agree";  
                checkBox1.Checked = false;  
                panel2.Controls.Add(checkBox1);  
                tabPage1.Controls.Add(panel1);  
      
                tabPage1.Controls.Add(panel2);  
            }  
      
            private void Panel2_Click(object sender, EventArgs e)  
            {  
                panel1.Size = new Size(tabPage1.Width, (tabPage1.Height / 3));  
                panel2.Size = new Size(tabPage1.Width, (tabPage1.Height / 3)*2);  
                panel2.Location = new Point(panel1.Left, panel1.Bottom);  
            }  
      
            private void Panel1_Click(object sender, EventArgs e)  
            {  
                panel1.Size = new Size(tabPage1.Width, (tabPage1.Height / 3) * 2);  
                panel2.Size = new Size(tabPage1.Width, (tabPage1.Height / 3));  
                panel2.Location = new Point(panel1.Left, panel1.Bottom);  
      
            }  
    

    Result:

    200717-2.gif

    As the above picture shown, when I click panel1, panel1 will expand and panel2 will reduce, so on the panel2.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    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

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.